前面在作東西的時候都用到了storyboard,在今天的代碼中就純手寫代碼本身用封裝個Button。這個Button繼承於UIView類,在封裝的時候用上啦OC中的三種回調模式:目標動做回調,委託回調,Block回調。具體的內容請參考以前的博客:「Objective-C中的Block回調模式」,「Target-Action回調模式」,「Objective-C中的委託(代理)模式」。在接下來要封裝的button中將要用到上面的知識點。以前在作新浪微博中的Cell的時候用到了Block回調來肯定是那個Cell上的那個Button。html
在封裝Button以前呢,簡單的瞭解一下UIView中的觸摸事件:post
1.當觸摸開始時會調用下面的事件atom
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)eventspa
2.當觸摸取消時會調用下面的事件3d
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event代理
3.當觸摸結束時會調用下面的事件日誌
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)eventcode
4.當觸摸移動時會調用下面的事件orm
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)eventhtm
因此在封裝本身的button是咱們會用上上面的方法,首先新建一個ViewController, 而後把咱們新建的ViewController在AppDelegate.m中設置成咱們的根視圖,咱們關於Button的初始化和配置都寫在ViewController中的ViewDidLoad中代碼以下:
1 MyViewController *myViewController = [[MyViewController alloc] init]; 2 self.window.rootViewController = myViewController;
1、目標動做回調:
首先新建一個MyButton類,MyButton類繼承於UIView, 咱們就在MyButton類中自定義咱們的button.下面要爲自定義Button添加目標動做回調接口,步驟以下:
1.在MyButton.h中聲明目標動做註冊方法:
//TargetAction回調 -(void)addTarget:target action:(SEL)action;
2.在MyButton.m中進行實現:
1 //延展 2 @interface MyButton() 3 4 @property (nonatomic,weak) id target; 5 @property (nonatomic, assign) SEL action; 6 7 @end 8 9 10 //實現 11 @implementation MyButton 12 //目標動做回調 13 -(void)addTarget:(id)target action:(SEL)action 14 { 15 self.target = target; 16 self.action = action; 17 }
3.經過target來執行action方法,觸摸完成的事件中讓target執行action方法,執行以前要判斷一下觸摸的釋放點是否在按鈕的區域內,代碼以下:
1 //當button點擊結束時,若是結束點在button區域中執行action方法 2 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 3 { 4 //獲取觸摸對象 5 UITouch *touche = [touches anyObject]; 6 //獲取touche的位置 7 CGPoint point = [touche locationInView:self]; 8 9 //判斷點是否在button中 10 if (CGRectContainsPoint(self.bounds, point)) 11 { 12 //執行action 13 [self.target performSelector:self.action withObject:self]; 14 } 15 16 }
4.在MyViewController中進行button的初始化,並註冊目標方法回調,當點擊button時,咱們MyViewController中的tapButton方法就會被執行:
1 //在v2中添加一個button 2 MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(10, 10, 44, 44)]; 3 4 button.backgroundColor = [UIColor blackColor]; 5 6 //註冊回調 7 [button addTarget:self action:@selector(tapButton)];
2、委託回調
1.在上面的基礎上添加上委託回調,經過委託回調添加按鈕是否可用,按鈕將要點擊和按鈕點擊後的事件,首先咱們得有協議來聲明這三個方法。協議咱們就不新建文件了,下面的協議是添加在MyButton.h中的,協議定義以下:
1 //定義MyButton要實現的協議, 用於委託回調 2 @protocol MyButtonDelegete <NSObject> 3 4 //可選擇的實現 5 @optional 6 7 //當button將要點擊時調用 8 -(void) myButtonWillTap:(MyButton *) sender; 9 10 //當button點擊後作的事情 11 -(void) myButtonDidTap: (MyButton *) sender; 12 13 //判斷button是否能夠被點擊 14 -(BOOL) myButtonShouldTap: (MyButton *) sender; 15 16 @end
2.在MyButton.h中添加delegate屬性,爲了不強引用循環,定義爲weak類型,用於回調的註冊:
//委託回調接口 @property (nonatomic, weak) id <MyButtonDelegete> delegate;
3.在MyButton.m中當開始點擊按鈕時作一下處理,首先得判斷delegate對象是否實現了協議中的方法若是實現了就經過delegate回調,若是沒實現就不調用
2 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 3 { 4 5 //判斷myButtonShouldTap是否在degate中實現啦:委託回調 6 if ([self.delegate respondsToSelector:@selector(myButtonShouldTap:)]) 7 { 8 //若是實現了,就獲取button的狀態 9 myButtonState = [self.delegate myButtonShouldTap:self]; 10 11 } 12 13 //根據按鈕的狀態來作處理 14 if (myButtonState) 15 { 16 //若是myButtonWillTap被實現啦,此時咱們就實現myButtonWillTapf方法 17 if ([self.delegate respondsToSelector:@selector(myButtonWillTap:)]) 18 { 19 [self.delegate myButtonWillTap:self]; 20 } 21 } 22 }
4.在touchesEnded中相應的位置添加以下代碼去執行按鈕點擊時要回調的方法:
1 //點擊結束要調用myButtonDidTap 委託回調 2 if ([self.delegate respondsToSelector:@selector(myButtonDidTap:)]) 3 { 4 [self.delegate myButtonDidTap:self]; 5 }
五、在MyViewController.m中註冊委託回調
1 //註冊委託回調 2 button.delegate = self;
六、MyViewController要實現MyButtonDelegate,並實現相應的方法
1 //實現button委託回調的方法myButtonShouldTap:設置button是否好用 2 -(BOOL) myButtonShouldTap:(MyButton *)sender 3 { 4 NSLog(@"我是Delegate:should方法"); 5 return YES; 6 } 7 8 //實現按鈕將要點擊的方法 9 -(void)myButtonWillTap:(MyButton *)sender 10 { 11 NSLog(@"我是Delegate: will方法"); 12 } 13 14 //實現按鈕點擊完要回調的方法 15 -(void) myButtonDidTap:(MyButton *)sender 16 { 17 NSLog(@"我是Delegate: Did"); 18 }
三.Block回調
一、爲咱們的按鈕添加Block回調(把上面的委託回調改爲Block回調),和以前微博中的Cell的Block回調相似,首先在MyButton.h中聲明咱們要用的Block類型,而後提供Block的set方法:
//button中使用Block回調,定義Block類型 @class MyButton; typedef void (^ButtonWillAndDidBlock) (MyButton *sender); typedef BOOL (^ButtonShouldBlock) (MyButton *sender); //接受block的方法 -(void)setButtonShouldBlock: (ButtonShouldBlock) block; -(void)setButtonWillBlock: (ButtonWillAndDidBlock) block; -(void)setButtonDidBlock:(ButtonWillAndDidBlock) block;
2.在MyButton.m中的延展中添加相應的屬性來接受Controller中傳過來的Block
1 //接受block塊 2 @property (nonatomic, strong) ButtonWillAndDidBlock willBlock; 3 @property (nonatomic, strong) ButtonWillAndDidBlock didBlock; 4 @property (nonatomic, strong) ButtonShouldBlock shouldBlock;
3.實現setter方法
1 //實現block回調的方法 2 -(void)setButtonWillBlock:(ButtonWillAndDidBlock)block 3 { 4 self.willBlock = block; 5 } 6 7 -(void)setButtonDidBlock:(ButtonWillAndDidBlock)block 8 { 9 self.didBlock = block; 10 } 11 12 -(void) setButtonShouldBlock:(ButtonShouldBlock)block 13 { 14 self.shouldBlock = block; 15 }
4.在MyButton.m中有委託調用的地方加入相應的Block回調,添加的代碼以下:
1 //block回調 2 if (self.shouldBlock) { 3 //block回調獲取按鈕狀態 4 myButtonState = self.shouldBlock(self); 5 } 6 7 8 //block回調實現willTap 9 if (self.willBlock) 10 { 11 self.willBlock(self); 12 } 13 14 15 //block回調 16 if (self.didBlock) { 17 self.didBlock(self); 18 }
五、在MyViewController中調用Button中的setter方法傳入相應的block:
1 2 //實現button的block回調 3 [button setButtonShouldBlock:^BOOL(MyButton *sender) { 4 NSLog(@"我是Block: should方法\n\n"); 5 return YES; 6 }]; 7 8 [button setButtonWillBlock:^(MyButton *sender) { 9 NSLog(@"我是Block: Will方法\n\n"); 10 }]; 11 12 [button setButtonDidBlock:^(MyButton *sender) { 13 NSLog(@"我是Blcok: Did方法\n\n"); 14 }]; 15 16 17 [self.view addSubview:button];
通過上面的代碼咱們的button就擁有三種回調模式了,下面是點擊button控制檯輸出的日誌: