假设我有一个UIViewController子类,照顾一些UIViews。这些UIViews添加为UIViewController的view
属性的子视图):UIView,其superview和touchesBegan:
的UIViewController:
- (void)viewDidLoad {
UIImageView *smallImageView...
[self.view addSubview:smallImageView];
UIButton *button..
[self.view addSubview:button];
UIView *bigUIView.. // covers the entire screen (frame = (0.0, 0.0, 1024.0, 768.0))
[self.view addSubview:bigUIView];
...
}
据我所知,因为bigUIView
是最前面的视图,并覆盖整个屏幕,它将接收touchesBegan:withEvent:
和其他视图如button
将不会收到任何触摸事件。
在我的应用程序bigUIView
必须是最顶层的视图,因为它包含主要的用户界面对象(CALayers,实际上,主要的游戏对象),当动画时,必须在所有其他辅助UI元素(UIButtons等)。但是,我希望能够在Responder Chain中保留UIButtons和其他对象。
我试图实现在bigUIView
类以下内容:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview touchesBegan:touches withEvent:event];
... hit test the touch for this specific uiview..
}
注:
bigUIView
的上海华指UIViewController的view
财产,触摸事件它传播到它的所有子视图?bigUIView
必须有userInteractionEnabled = YES
,因为它也处理用户输入。- 我不能将
button
/smallImageView
放在前面,因为它会出现在主要游戏对象(子层bigUIView
)的上方。
在此先感谢。
谢谢jlehr,`pointInside:withEvent:`是解决方案。当点位于其他子视图的框架矩形内时,通过返回NO,会导致不调用bigUIView的`-touchesBegan:withEvent:`方法,并触发`button`的操作。请注意,这会导致`bigUIView`和`button`之间的耦合。当bigUIView覆盖整个屏幕时,我如何才能在`UIViewController`中拦截事件阶段方法?在`bigUIView`类中创建一个不同的方法签名,并通过“catch all”UIViewController类来委托它? – 2011-02-07 16:45:49
不,不要在`bigUIView`中实现事件阶段方法。这样,消息将被转发到响应者链,直到它们到达具有重载实现的对象(例如`UIButton`或您的自定义`UIViewController`子类)。 – jlehr 2011-02-07 17:09:01