前面在做東西的時候都用到了storyboard,在今天的代碼中就純手寫代碼自己用封裝個Button。這個Button繼承于UIView類,在封裝的時候用上啦OC中的三種回調(diào)模式:目標(biāo)動作回調(diào),委托回調(diào),Block回調(diào)。具體的內(nèi)容請參考之前的博客:“Objective-C中的Block回調(diào)模式”,“Target-Action回調(diào)模式”,“Objective-C中的委托(代理)模式”。在接下來要封裝的button中將要用到上面的知識點。之前在做新浪微博中的Cell的時候用到了Block回調(diào)來確定是那個Cell上的那個Button。
在封裝Button之前呢,簡單的了解一下UIView中的觸摸事件:
1.當(dāng)觸摸開始時會調(diào)用下面的事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
2.當(dāng)觸摸取消時會調(diào)用下面的事件
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
3.當(dāng)觸摸結(jié)束時會調(diào)用下面的事件
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
4.當(dāng)觸摸移動時會調(diào)用下面的事件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
所以在封裝自己的button是我們會用上上面的方法,首先新建一個ViewController, 然后把我們新建的ViewController在AppDelegate.m中設(shè)置成我們的根視圖,我們關(guān)于Button的初始化和配置都寫在ViewController中的ViewDidLoad中代碼如下:
1 MyViewController *myViewController = [[MyViewController alloc] init];2 self.window.rootViewController = myViewController;
一、目標(biāo)動作回調(diào):
首先新建一個MyButton類,MyButton類繼承于UIView, 我們就在MyButton類中自定義我們的button.下面要為自定義Button添加目標(biāo)動作回調(diào)接口,步驟如下:
1.在MyButton.h中聲明目標(biāo)動作注冊方法:
//TargetAction回調(diào)-(void)addTarget:target action:(SEL)action;
2.在MyButton.m中進(jìn)行實現(xiàn):
1 //延展 2 @interface MyButton() 3 4 @PRoperty (nonatomic,weak) id target; 5 @property (nonatomic, assign) SEL action; 6 7 @end 8 9 10 //實現(xiàn)11 @implementation MyButton12 //目標(biāo)動作回調(diào)13 -(void)addTarget:(id)target action:(SEL)action14 {15 self.target = target;16 self.action = action;17 }
3.通過target來執(zhí)行action方法,觸摸完成的事件中讓target執(zhí)行action方法,執(zhí)行之前要判斷一下觸摸的釋放點是否在按鈕的區(qū)域內(nèi),代碼如下:
1 //當(dāng)button點擊結(jié)束時,如果結(jié)束點在button區(qū)域中執(zhí)行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 //執(zhí)行action13 [self.target performSelector:self.action withObject:self]; 14 }15 16 }
4.在MyViewController中進(jìn)行button的初始化,并注冊目標(biāo)方法回調(diào),當(dāng)點擊button時,我們MyViewController中的tapButton方法就會被執(zhí)行:
1 //在v2中添加一個button2 MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(10, 10, 44, 44)];3 4 button.backgroundColor = [UIColor blackColor];5 6 //注冊回調(diào)7 [button addTarget:self action:@selector(tapButton)];
二、委托回調(diào)
1.在上面的基礎(chǔ)上添加上委托回調(diào),通過委托回調(diào)添加按鈕是否可用,按鈕將要點擊和按鈕點擊后的事件,首先我們得有協(xié)議來聲明這三個方法。協(xié)議我們就不新建文件了,下面的協(xié)議是添加在MyButton.h中的,協(xié)議定義如下:
1 //定義MyButton要實現(xiàn)的協(xié)議, 用于委托回調(diào) 2 @protocol MyButtonDelegete <NSObject> 3 4 //可選擇的實現(xiàn) 5 @optional 6 7 //當(dāng)button將要點擊時調(diào)用 8 -(void) myButtonWillTap:(MyButton *) sender; 9 10 //當(dāng)button點擊后做的事情11 -(void) myButtonDidTap: (MyButton *) sender;12 13 //判斷button是否可以被點擊14 -(BOOL) myButtonShouldTap: (MyButton *) sender;15 16 @end
2.在MyButton.h中添加delegate屬性,為了避免強(qiáng)引用循環(huán),定義為weak類型,用于回調(diào)的注冊:
//委托回調(diào)接口@property (nonatomic, weak) id <MyButtonDelegete> delegate;
3.在MyButton.m中當(dāng)開始點擊按鈕時做一下處理,首先得判斷delegate對象是否實現(xiàn)了協(xié)議中的方法如果實現(xiàn)了就通過delegate回調(diào),如果沒實現(xiàn)就不調(diào)用
2 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 3 { 4 5 //判斷myButtonShouldTap是否在degate中實現(xiàn)啦:委托回調(diào) 6 if ([self.delegate respondsToSelector:@selector(myButtonShouldTap:)]) 7 { 8 //如果實現(xiàn)了,就獲取button的狀態(tài) 9 myButtonState = [self.delegate myButtonShouldTap:self];10 11 } 12 13 //根據(jù)按鈕的狀態(tài)來做處理14 if (myButtonState)15 {16 //如果myButtonWillTap被實現(xiàn)啦,此時我們就實現(xiàn)myButtonWillTapf方法17 if ([self.delegate respondsToSelector:@selector(myButtonWillTap:)])18 {19 [self.delegate myButtonWillTap:self];20 }21 }22 }
4.在touchesEnded中相應(yīng)的位置添加如下代碼去執(zhí)行按鈕點擊時要回調(diào)的方法:
1 //點擊結(jié)束要調(diào)用myButtonDidTap 委托回調(diào)2 if ([self.delegate respondsToSelector:@selector(myButtonDidTap:)])3 {4 [self.delegate myButtonDidTap:self];5 }
5、在MyViewController.m中注冊委托回調(diào)
1 //注冊委托回調(diào)2 button.delegate = self;
6、MyViewController要實現(xiàn)MyButtonDelegate,并實現(xiàn)相應(yīng)的方法
1 //實現(xiàn)button委托回調(diào)的方法myButtonShouldTap:設(shè)置button是否好用 2 -(BOOL) myButtonShouldTap:(MyButton *)sender 3 { 4 NSLog(@"我是Delegate:should方法"); 5 return YES; 6 } 7 8 //實現(xiàn)按鈕將要點擊的方法 9 -(void)myButtonWillTap:(MyButton *)sender10 {11 NSLog(@"我是Delegate: will方法");12 }13 14 //實現(xiàn)按鈕點擊完要回調(diào)的方法15 -(void) myButtonDidTap:(MyButton *)sender16 {17 NSLog(@"我是Delegate: Did");18 }
三.Block回調(diào)
1、為我們的按鈕添加Block回調(diào)(把上面的委托回調(diào)改成Block回調(diào)),和之前微博中的Cell的Block回調(diào)類似,首先在MyButton.h中聲明我們要用的Block類型,然后提供Block的set方法:
//button中使用Block回調(diào),定義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中的延展中添加相應(yīng)的屬性來接受Controller中傳過來的Block
1 //接受block塊2 @property (nonatomic, strong) ButtonWillAndDidBlock willBlock;3 @property (nonatomic, strong) ButtonWillAndDidBlock didBlock;4 @property (nonatomic, strong) ButtonShouldBlock shouldBlock;
3.實現(xiàn)setter方法
1 //實現(xiàn)block回調(diào)的方法 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)block13 {14 self.shouldBlock = block;15 }
4.在MyButton.m中有委托調(diào)用的地方加入相應(yīng)的Block回調(diào),添加的代碼如下:
1 //block回調(diào) 2 if (self.shouldBlock) { 3 //block回調(diào)獲取按鈕狀態(tài) 4 myButtonState = self.shouldBlock(self); 5 } 6 7 8 //block回調(diào)實現(xiàn)willTap 9 if (self.willBlock)10 {11 self.willBlock(self);12 }13 14 15 //block回調(diào)16 if (self.didBlock) {17 self.didBlock(self);18 }
5、在MyViewController中調(diào)用Button中的setter方法傳入相應(yīng)的block:
1 2 //實現(xiàn)button的block回調(diào) 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];
經(jīng)過上面的代碼我們的button就擁有三種回調(diào)模式了,下面是點擊button控制臺輸出的日志:
新聞熱點
疑難解答