由上图可以看出当点击屏幕的时候,会从UIWindow通过hitTest:withEvent方法返回被点击的view。hitTest内部是个递归的调用过程,通过pointInside:withEvent返回YES/NO,来判断点击的point是否在当前view上,如果YES:如果有子视图,则倒序遍历subviews(最后添加的view先调用)调用hitTest:withEvent,找到子view返回;如果没有子视图则返回当前view。
show me the code
我们通过实际的使用来加深理解:
要求,我们实现个button点击内切圆以外的区域不响应。
我们定义一个button继承于UIButton,重写hitTest:withEvent方法(其实,主要是实现里边的pointInside:withEvent方法):
@implementation CustomButton
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.userInteractionEnabled ||
[self isHidden] ||
self.alpha <= 0.01) {
return nil;
}
if ([self pointInside:point withEvent:event]) {
//遍历当前对象的子视图
__block UIView *hit = nil;
[self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 坐标转换
CGPoint vonvertPoint = [self convertPoint:point toView:obj];
//调用子视图的hittest方法
hit = [obj hitTest:vonvertPoint withEvent:event];
// 如果找到了接受事件的对象,则停止遍历
if (hit) {
*stop = YES;
}
}];
if (hit) {
return hit;
}
else{
return self;
}
}
else{
return nil;
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat x1 = point.x;
CGFloat y1 = point.y;
CGFloat x2 = self.frame.size.width / 2;
CGFloat y2 = self.frame.size.height / 2;
double dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
// 67.923
if (dis <= self.frame.size.width / 2) {
return YES;
}
else{
return NO;
}
}
@end