模拟UIView类的-hitTest:withEvent:方法系统实现

代码:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
        return nil;
    }
    
    if ([self pointInside:point withEvent:event]) {
        for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
            CGPoint convertedPoint = [subview convertPoint:point fromView:self];
            UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
            if (hitTestView) {
                return hitTestView;
            }
        }
        return self;
    }
    
    return nil;
}

 

转载于:https://www.cnblogs.com/xwoder/p/6242074.html

你可能感兴趣的:(模拟UIView类的-hitTest:withEvent:方法系统实现)