2017-10-13 92 views
0

我在處理指定視圖邊界外的觸摸時遇到了上述問題。我在網站上找到了解決辦法告訴我重寫則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

回答

0

你理解錯了,super不會在子視圖上調用hitTest方法,它會調用pointInside方法。 從documentation:withEvent:方法:

此方法通過調用 pointInside橫穿視圖層級中的每個子視圖的方法,以確定哪個子視圖 應該接收的觸摸事件。

希望它有幫助!

相關問題