(UIView*)viewController中的hitTest?

sen*_*thu 8 iphone ipad

是否可以使用

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)

在viewcontroller.m文件中?任何教程请?

Vla*_*mir 11

hitTest:withEvent: 方法在UIView类中声明,因此您不能直接在UIViewController子类中使用它.

但是touchesBegan:withEvent:UIResponder中声明了其他触摸跟踪方法,因此可以在UIView和UIViewController子类中实现.


joh*_*ope 5

也许另一种解决方法是简单地将UIView子类化,并且在视图控制器的init函数中将视图控制器视图分配给该UIView.

例如.NavPanelViewController.m

- (id)init

    theView = [[NavPanelView alloc]init]; // declare theView in your header and in dealloc release
    self.view = theView;
Run Code Online (Sandbox Code Playgroud)

然后,您可以覆盖子类中的pointInside方法.

  • 最佳做法是在`-loadView`中设置自定义UIView.原因是要避免过早加载视图. (4认同)