为什么调用super的hitTest不会导致无限循环?

为什么调用super的hitTest不会导致无限循环?

问题描述:

我在处理指定视图边界外的触摸时遇到了上述问题。我在网站上找到了解决办法告诉我重写则hitTest:事件:方法是这样的:为什么调用super的hitTest不会导致无限循环?

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
UIView *view = [super hitTest:point withEvent:event]; 
if (view == nil) { 
    for (UIView *subView in self.subviews) { 
     CGPoint tp = [subView convertPoint:point fromView:self]; 
     if (CGRectContainsPoint(subView.bounds, tp)) { 
      view = subView; 
     } 
    } 
} 
return view; 

}

我注意到,在第一线的作者所说[super hitTest:point withEvent:event],我也知道命中测试是递归的。所以super必须调用子视图的hitTest方法,而后者会再次调用super。我只是想知道为什么它不会导致无限循环?谢谢!

+0

以同样的方式viewDidLoad()不会导致无限循环。 –

+0

你不是在混合子**视图**和子**类**吗? –

+0

谢谢。我真的误解了这一点。我只是简单地混合了super和superView。对于那个很抱歉。 – Stephen

你理解错了,super不会在子视图上调用hitTest方法,它会调用pointInside方法。 从documentation:withEvent:方法:

此方法通过调用 pointInside横穿视图层级中的每个子视图的方法,以确定哪个子视图 应该接收的触摸事件。

希望它有帮助!