一、系统是怎么找到第一响应者的? --只通过UIView及其子类查找
0 调用根视图的hitTtest:withEvent,其的执行过程如下:
- Ie calls pointInside:withEvent:of self
- If the return is NO,
hitTest:withEvent:
returnsnil
. the end of the story. - If the return is YES, it sends
hitTest:withEvent:
messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil
object, or all subviews receive the message. - If a subview returns a non-
nil
object in the first time, the firsthitTest:withEvent:
returns that object. the end of the story. - If no subview returns a non-
nil
object, the firsthitTest:withEvent:
returnsself
参考: 这里
二 、触摸事件是如何传递的?
三、hitTest:withEvent应用:
1)父视图中有布局重叠的且都可响应用户操作的对象,如:ScrollView and Button,如果Button在ScrollView下面,正常情况下Button是不会成为第一响应者的,如果想让Button可以响应在其布局内的触摸事件,可以在Button和ScrollView的父View中重写hitTest:withEvent方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGPoint hitPoint = [_testButton convertPoint:point fromView:self]; if ([_testButton pointInside:hitPoint withEvent:event]) return _testButton; return [super hitTest:point withEvent:event]; }//_testButton是指定响应对象的 弱 引用
2)UIView的子类不响应触摸事件,但其子View可以响应。通过设置userInteractionEnabled=NO,可以使UIView子类不响应触摸事件,但其会挟持子View,原因见3)
这时,可以通过重写hitTest:withEvent来实现:
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
id hitView = [super hitTest:point withEvent:event];
if (hitView == self) return nil;
else return hitView;
}
参考: 这里
3) userInteractionEnabled = NO的作用:使当前的hitTest:withEvent返回nil,其它的类似属性还有:Hidden=YES,alpha<0.01,(UIControl中Enabled=NO??),事件发生的点在子View的几何范围内,却超过了父View的几何范围( clipsToBounds=NO时可出现此种情况 )
一个案例:
我的UIScrollViewOut中嵌入了一个UIScrollViewIn,
当我想要拖动UIScrollViewIn时,UIScrollViewOut却动了,而UIScrollViewIn没有动。但是只要按住UIScrollViewIn一会再拖动,UIScrollViewIn就可以滑动。
网上查阅UIScrollView的原理:
UIScrollView重载了hitTest方法,当手指touch的时候,UIScrollView会拦截所有event,然后等待150ms,在这段时间内,如果没有手指没有移动,当时间结束时,UIScrollView会发送tracking event到子视图上,并且自身不滑动。在时间结束前,手指发生了移动,那么UIScrollView就会进行滑动,从而取消发送tracking。
看来是UIScrollViewOut的问题。直接拖动UIScrollViewIn,此时touch时间在150ms以内,UIScrollViewOut会认为是拖动自己,从而拦截了event,导致UIScrollViewIn接受不到滑动的event。但是只要按住UIScrollViewIn一会再拖动,此时此时touch时间超过150ms,因此滑动的event会发送到UIScrollViewIn上。
期间试过几种方法,只有一种可行,就是重写UIScrollViewOut的hitTest方法:当滑动UIScrollViewIn时,使UIScrollViewOut不可滑动。
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView* result = [super hitTest:point withEvent:event];
if ([result.superview isKindOfClass:[UIScrollViewIn class]])
{
self.scrollEnabled = NO;
}
else
{
self.scrollEnabled = YES;
}
return result;
}
PS:
试过几种方法都不行,例如:修改UIScrollViewIn为firstResponder;重写UIScrollViewOut中hitTest方法,永远返回UIScrollViewIn的指针