IOS開發之自定義Button(集成三種回調模式)


  前面在做東西的時候都用到了storyboard,在今天的代碼中就純手寫代碼自己用封裝個Button。這個Button繼承於UIView類,在封裝的時候用上啦OC中的三種回調模式:目標動作回調,委托回調,Block回調。具體的內容請參考之前的博客:“Objective-C中的Block回調模式”,“Target-Action回調模式”,“Objective-C中的委托(代理)模式”。在接下來要封裝的button中將要用到上面的知識點。之前在做新浪微博中的Cell的時候用到了Block回調來確定是那個Cell上的那個Button。

  在封裝Button之前呢,簡單的了解一下UIView中的觸摸事件:

    1.當觸摸開始時會調用下面的事件

      -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

     2.當觸摸取消時會調用下面的事件

      -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

       3.當觸摸結束時會調用下面的事件

      -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

    4.當觸摸移動時會調用下面的事件

      -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

  所以在封裝自己的button是我們會用上上面的方法,首先新建一個ViewController, 然后把我們新建的ViewController在AppDelegate.m中設置成我們的根視圖,我們關於Button的初始化和配置都寫在ViewController中的ViewDidLoad中代碼如下:

1    MyViewController *myViewController = [[MyViewController alloc] init];
2    self.window.rootViewController = myViewController;

 

  一、目標動作回調:

    首先新建一個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)];

 

  二、委托回調 

   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         }

 

 

    5、在MyViewController.m中注冊委托回調

1     //注冊委托回調
2     button.delegate = self;

 

    6、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回調

    1、為我們的按鈕添加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         }

 

   5、在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控制台輸出的日志:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM