前几天在一个iOS开发交流群中看到这样一道笔试题(如图一所示)。大致意思就是三个View:A、B、C,A在最底层,B在中间,C在最上层,B和C都是A的subView,一个tap事件发生在C范围内,设C的userInteractionEnabled = NO,那么接下来由谁来响应该tap事件?这道题比较基础,然而群里面各种答案都有。这说明我们一部分开发者或多或少存在一些知识盲点,当然这其中也不乏做了两三年开发的iOS开发者。大家可以先做一下。
我们都知道这道题考查对响应者链的理解,先来回顾一下事件处理的一般流程。一般的,当用户点击屏幕时会产生一个触摸事件,系统将该事件加入到UIApplication管理的事件队列之中,然后UIApplication将事件进行分发处理。通常会将事件先分发给主窗口(UIWindow)处理,主窗口会在view hierarchy中找到最合适的一个View来处理该事件。在这个过程中UIWindow会调用UIVIew的hitTest:withEvent:方法。参考官方文档Event Handling Guide for UIKit Apps中的Determining Which Responder Contained a Touch Event这段。
UIKit uses view-based hit testing to determine where touch events occur. Specifically, UIKit compares the touch location to the bounds of view objects in the view hierarchy. The hitTest:withEvent: method of UIView walks the view hierarchy, looking for the deepest subview that contains the specified touch. That view becomes the first responder for the touch event.
NOTE
If a touch location is outside of a view’s bounds, the hitTest:withEvent: method ignores that view and all of its subviews. As a result, when a view’s clipsToBounds property is NO, subviews outside of that view’s bounds are not returned even if they happen to contain the touch. For more information about the hit testing behavior, see the discussion of the hitTest:withEvent: method in UIView.
现在我们来看看UIView的这个hitTest:withEvent:方法。这个方法的处理流程大致如下:
首先会调用当前view的pointInside: withEvent:方法。若返回NO,则point不在当前view内,此时hitTest:withEvent:方法返回nil;若返回YES,则point在当前view内。接着会遍历当前view的subViews,subView也会调用UIView的hitTest:withEvent:方法。(注意:这个遍历subView的顺序是由top到bottom的,可以理解为一个栈,先调用数组末尾的,依次向前遍历)。最终,如果第一次就有subView的hitTest:withEvent:方法返回非空对象,则当前view的hitTest:withEvent:方法就返回此非空对象,处理结束;如果所有的subView的hitTest:withEvent:方法都返回nil,那么当前view的hitTest:withEvent:方法直接返回自身(self),处理结束。
在这个处理过程中,UIView的hitTest:withEvent:方法会忽略以下View:(参考hitTest:withEvent:方法说明)
1)隐藏的view(hidden = YES)
2)禁止了用户操作的view(userInteractionEnabled = NO)
3)alpha < 0.01的view
4)view超出parentView的bounds,且point不在parentView内。
This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.
Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
到这里想必都知道这道笔试题的答案了。写了个简单的例子供大家参考。首先创建了三个view:redView、greenView和blueView,分别对应本道题中的viewA、B、C(如图二所示)。
然后为三个view分别添加一个UITapGestureRecognizer
UITapGestureRecognizer *tapRed = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRedView)];
[self.redView addGestureRecognizer:tapRed];
UITapGestureRecognizer *tapGreen = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGreenView)];
[self.greenView addGestureRecognizer:tapGreen];
UITapGestureRecognizer *tapBlue = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlueView)];
[self.blueView addGestureRecognizer:tapBlue];
对应的方法实现很简单,就是Log一句话。
- (void)tapRedView {
NSLog(@"tapped redView");
}
- (void)tapGreenView {
NSLog(@"tapped greenView");
}
- (void)tapBlueView {
NSLog(@"tapped blueView");
}
最后在三个view中分别覆写hitTest:withEvent:方法和pointInside: withEvent:方法,点击blueView对以上各种情况进行一一证实。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
NSLog(@"%@--hitTest,view = %@",NSStringFromClass([self class]),view);
return view;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
BOOL result = [super pointInside:point withEvent:event];
NSLog(@"%@--pointInside,result = %d",NSStringFromClass([self class]),result);
return result;
}
图三是测试hidden时的截图。此时blueView.hidden = YES,点击了blueView中的某个point,该point也在greenView的bounds内,可以看到最后响应事件的greenView,而不是blueView,也就是上面四种情况中的第一种情况:hitTest:withEvent:方法会忽略隐藏的view。其它情况请参考完整代码(https://github.com/FreakLee/UnderstandingHitTest.git)进行逐一理解。
至此,UIView的hitTest:withEvent:方法讲解完毕。当然在iOS开发中,我们利用这个方法处理很多事情,比如扩展UIControl的响应区域,subView超出parentView的bounds时也让其响应事件。具体实现不在本文的讨论范围之内。
参考文献:
Event Handling Guide for UIKit Apps:developer.apple.com/library/content/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/HandlngEventsUsingtheResponderChain.html#//apple_ref/doc/uid/TP40009541-CH11-SW1
UIKit Api Reference:developer.apple.com/reference/uikit/uiview/1622469-hittest