概念
响应者:对用户交互动作事件进行相应的对象。
响应者链:称为处理事件的响应者的限售顺序链。
正文
1、Hit-Test机制
当用户触摸(touch)屏幕进行交互式,系统首先要找到响应者(Responder)。系统检测到手指触摸(Touch)操作时,将Touch以UIEvent方式加入UIApplication事件队列中。UIApplication从事件队列中取出最新的触摸事件进行分发传递到UIWindow进行处理。UIWindow会通过hitTest:withEvent:方法寻找触碰点所在的视图,这个过程称之为hit->test->view。
hittest的顺序如下
UIApplication->UIWindow->Root View->....->subView
在顶级视图(Root View)上调用pointInside:withEvent:方法判断触摸点是否在当前视图内。
如果返回NO,那么hitTest:withEvent:返回nil;
如果返回YES,那么它会向当前视图的所有子视图发送hitTEst: withEvetn:消息,所有姿势图的遍历顺序是从最顶层视图一直到最底层视图,即从subViews的数组的某位向前遍历,直到有子视图返回非空对象或者全部子视图遍历完毕。
如果有subview的hitTest:withEvent:返回非空对象则A返回此对象,处理结束(注意这个过程,子视图也是根据pointInside:withEvent:的返回值来确定是返回空还是当前子视图对象的。并且这个过程中如果子视图的hidden=YES、userInteractionEnabled=NO或者alpha小于0.1都会并忽略);如果所有的subview遍历结束仍然没有返回非空对象,则hitTest:withEvent:返回self;系统就是这样通过hittest找到触碰到的视图(initialView)进行响应。
2、响应者链
有些时候,Touch后系统通过hittest机制找到了触碰到的inititalView,但是Initial view并没有或者无法正常处理此次Touch。这个时候,系统便会通过响应者链寻找下一个响应者,以对此次Touch进行响应。
响应者链书序如下:
InitialVIew->View Controller -> superview -> ... -> rootView -> UIWindow-> UIApplication
0064cTs2gw1f5btrssp5mj306y07dweh.jpg
如果有一个View有一个视图控制器(ViewController),它的下一个响应者是这个视图控制器,紧接着才是它的俯视图(SuperView),如果一直到RootView都没有处理这个事件,事件会传递到UIWindow(iOS中有一个单例Window),此时Widnow如果也没有处理事件,便进入UIApplication,UIApplication是一个响应者链的终点,它的下一个响应者指向nil,以结束整个循环。
3、实际开发中常见的相关问题
在实际开发中,经常会遇到视图没有响应的情况,特别是新手经常搞不清楚状况。
以下是视图没有响应的几个情况:
1.userInteractionEnabled=NO;
2.hidden=YES;
3.alpha=0~0.01;
4.没有实现touchesBegan:withEvent:方法,直接执行touchesMove:withEvent:等方法
5.目标视图点击区域不再俯视图的Frame上(superView背景色为clearColor的时候经常会忽略这个问题)。
在某些情境下,我们在点击子视图的时候仍然需要调用俯视图的touchesBegan:withEvent:等方法,例如我们在父视图上添加一个一个覆盖范围了父视图大部分面积的TableView或ScrollerView或者其他View,二我们要通过父视图的touchesBegaan:withEvent:方法来接受键盘。
这个时候我们可以通过UIView的类别,重写touch相关方法。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if (self.enableNextResponder) {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (self.enableNextResponder) {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (self.enableNextResponder) {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (self.enableNextResponder) {
[[self nextResponder] touchesCancelled:touches withEvent:event];
[super touchesCancelled:touches withEvent:event];
}
}












网友评论