参考:http://www.javashuo.com/article/p-rigmnzyj-hm.html
先实验变乱链,找到符合的view,在实验相应链。
一、变乱链
UIApplication -> window -> view -> view ……..->view
a. 当iOS步调中发生触摸变乱后,体系会将变乱到场到UIApplication管理的一个使命队列中
b. UIAplication 将处于使命队列最前端的变乱向下分发,即UIWindow
c. UIWindow 将变乱向下分发,即UIView
d. UIView起首看本身是否能处理处罚变乱(hidden = NO, userInteractionEnabled = YES, alpha >= 0.01),触摸点是否在本身身上。如果能,那么继续探求子视图
e. 遍历子控件(从后往前遍历),重复上面的步调。
f. 如果没有找到,那么本身就是变乱处理处罚着,如果本身不能处理处罚,那就不做任何事
- 变乱链的过程实在就是 - (UIView *)hitTestCGPoint)point withEventUIEvent *)event
函数的实验过程。
二、相应链
相应链是从最符合的view开始转达,处理处罚变乱转达给下一个相应者,相应链的转达是个变乱链转达相反的。如果全部相应者都不处理处罚变乱,则变乱被抛弃。通常获取级相应者是通过nextResponder方法的。
- 通常找到对应的view之后然后查找此view是否能处理处罚此变乱。
实在也就是看view能否依次相应下面的几个方法(并不愿定是全部)来构成一个消息。
- (void)touchesBeganNSSet<UITouch *> *)touches withEventnullable UIEvent *)event;
- (void)touchesMovedNSSet<UITouch *> *)touches withEventnullable UIEvent *)event;
- (void)touchesEndedNSSet<UITouch *> *)touches withEventnullable UIEvent *)event;
- (void)touchesCancelledNSSet<UITouch *> *)touches withEventnullable UIEvent *)event;
比方:
1.按钮上添加手势情况1
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; [btn setTitle"按钮" forState:UIControlStateNormal]; btn.frame = CGRectMake(40, 200, 50, 30); btn.backgroundColor = [UIColor greenColor]; [self.view addSubview:btn]; [btn addTarget:self actionselector(btnAction) forControlEvents:UIControlEventTouchUpInside]; UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(tapAction1)]; [btn addGestureRecognizer:tap1];- (void)btnAction { NSLog(@"按钮点击的");}- (void)tapAction1 { NSLog(@"手势1");}点击按钮后实验的效果是:
手势1为什么呢?
由于当我们添加了UITapGestureRecognizer手势之后,当nextResponder为当前的viewcontroller时,它走touches的几个方法时先构成了tap手势的消息发送,因此打印的效果是手势1
2.按钮上添加手势情况2
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self actionselector(panAction)]; [btn addGestureRecognizer: pan];- (void)panAction { NSLog(@"手势");}给按钮上添加的是平移手势
当我们点击按钮时效果为
按钮点击的当我们平移按钮时效果为
手势3.按钮上添加手势情况3
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self actionselector(panAction)]; [btn addGestureRecognizer: pan]; UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(tapAction1)]; [btn addGestureRecognizer:tap1]; [btn addTarget:self actionselector(btnAction1) forControlEvents:UIControlEventTouchDragInside];- (void)tapAction { NSLog(@"手势");}- (void)tapAction1 { NSLog(@"手势1");}- (void)btnAction1 { NSLog(@"按钮点击的1");}当按钮的controlEvents为UIControlEventTouchDragInside时
,同时添加了平移和轻鼓掌势
当我们点击按钮时实验的是 轻鼓掌势
手势1当我们平移按钮时,平移的手势和按钮的相应都实验了
按钮点击的1手势 |