前面介紹了響應(yīng)的基礎(chǔ) 和 手勢(shì)的優(yōu)先級(jí)
http://www.reibang.com/p/1e549c0669d8
接下來(lái)我們看看 UIButton的時(shí)間是怎么過來(lái)的肪凛,還是自定義一個(gè)MyButton 召噩,并添加到 控制器的self.view 中;
@implementation MyButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self addTarget:self action:@selector(myBtnClick_UIControlEventTouchDown) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(myBtnClick_EventTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)myBtnClick_UIControlEventTouchDown {
NSLog(@"myBtnClick_UIControlEventTouchDown");
}
- (void)myBtnClick_EventTouchUpInside {
NSLog(@"myBtnClick_EventTouchUpInside");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"MyButton:%p touchesBegan",self);
// UIControl 類不會(huì)讓事件傳遞出去帆啃,直接 通過 [UIApplication SendAction:to:from:forEvent] 傳遞給 UIControlEventTouchDown 事件
[super touchesBegan:touches withEvent:event];
NSLog(@"MyButton:%p touchesBegan--2",self);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"MyButton:%p touchesMoved",self);
[super touchesMoved:touches withEvent:event];
NSLog(@"MyButton:%p touchesMoved---2",self);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"MyButton:%p touchesCancelled",self);
[super touchesCancelled:touches withEvent:event];
NSLog(@"MyButton:%p touchesCancelled--2",self);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"MyButton:%p touchesEnded",self);
// UIControl 類不會(huì)讓事件傳遞出去,而是通過 [UIApplication SendAction:to:from:forEvent]嗤放, 傳遞給自身的 UIControlEventTouchUpInside 事件
[super touchesEnded:touches withEvent:event];
NSLog(@"MyButton:%p touchesEnded--2",self);
}
@end
通過 上面的demo 調(diào)試氛魁,也可以更加清楚 UIButton的 響應(yīng) 和 手勢(shì)是不一樣的藏姐;
MyButton 里面的
[super touchesBegan:touches withEvent:event];
不會(huì)觸發(fā) 父視圖的 對(duì)應(yīng)的方法呀酸;而是 會(huì)通過 [UIApplication SendAction:to:from:forEvent] 傳遞給 UIControlEventTouchDown 事件
MyButton 里面的
[super touchesEnded:touches withEvent:event]
凉蜂,不會(huì)觸發(fā) 父視圖的 對(duì)應(yīng)的方法;而是 會(huì)[UIApplication SendAction:to:from:forEvent], 傳遞給自身的 UIControlEventTouchUpInside
窿吩;