- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

UIApplication管理事件队列,当事件发生时,UIApplication把事件依次向下传,直到找到最适合的控件未知。

控件是通过- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event来寻找的。比如如果这个方法返回self,则不会再往下找了。。。

 

UIApplication -> [UIWindow hitTest:withEvent:] -> subView hitTest:withEvent........

判断原则:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    
    // 1.判断当前控件能否接收事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
    
    // 2. 判断点在不在当前控件
    if ([self pointInside:point withEvent:event] == NO) return nil;
    
    // 3.从后往前遍历自己的子控件
    NSInteger count = self.subviews.count;
    
    for (NSInteger i = count - 1; i >= 0; i--) {
        UIView *childView = self.subviews[i];
        
        // 把当前控件上的坐标系转换成子控件上的坐标系
        CGPoint childP = [self convertPoint:point toView:childView];
        
        UIView *fitView = [childView hitTest:childP withEvent:event];
        
        
        if (fitView) { // 寻找到最合适的view
            return fitView;
        }
    }
    // 循环结束,表示没有比自己更合适的view
    return self;
}

 

转载于:https://www.cnblogs.com/yintingting/p/4552277.html

你可能感兴趣的:(- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event)