(UIView *)hitTest在viewcontroller?
问题描述:
是否有可能在viewcontroller.m文件中使用(UIView *)hitTest在viewcontroller?
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
?任何教程请问?
答
hitTest:withEvent:
方法在UIView类中声明,所以你不能直接在UIViewController子类中使用它。
但其他触摸跟踪方法,如touchesBegan:withEvent:
等在UIResponder声明,所以可以在两个UIView的实现和子类的UIViewController
答
你总是可以定义一个特定的委托处理相关则hitTest信息。在您的UIView类的.m(实现)文件中的hitTest:withEvent:
中调用委托的方法,将所有相关信息传递给委托来处理。声明你的ViewController类来实现这样的委托:
@implementation MyViewController: UIViewController <HitTestDelegate>
这样的逻辑下用户的敲击实际执行将在MyViewController类。
答
也许一个替代的解决方法是简单地为UIView子类,并在您的视图控制器的init函数分配视图控制器视图到该UIView。
例如, NavPanelViewController.m
- (id)init
theView = [[NavPanelView alloc]init]; // declare theView in your header and in dealloc release
self.view = theView;
然后你就可以覆盖在子类中pointInside方法。
我不知道你为什么不被接受的答案,因为它是唯一真正回答问题的答案。 – SpaceDog 2012-03-24 12:06:02
最佳做法是在'-loadView'中设置一个自定义的UIView。原因是为了避免过早加载视图。 – jonsibley 2014-01-09 17:44:15