假设我有一个UIViewController的子类,它负责一些UIView。这些UIView被添加为UIViewController的
据我所知,由于
在我的应用程序中,
我尝试在
view
属性的子视图:
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等)之上。但是,我仍然希望能够在响应链中拥有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
的子层)的上方。
提前感谢。
pointInside:withEvent:
是解决方案。当点在其他子视图的框架矩形内时返回NO,这会导致bigUIView
的-touchesBegan:withEvent:
方法不被调用,而触发button
的操作。请注意,这会导致bigUIView
和button
之间产生耦合。当bigUIView
覆盖整个屏幕时,我如何仅在UIViewController
中拦截事件阶段方法?在bigUIView
类中创建一个不同的方法签名,并由“catch all”UIViewController
类委托它? - Eduardo CoelhobigUIView
中实现事件阶段方法。这样,消息将会被转发到响应者链上直到它们到达具有重写实现的对象(例如UIButton
或您自定义的UIViewController
子类)。 - jlehrbigUIView
实例可以调用该方法进行实际决策。这样,视图控制器将负责计算哪个视图接收消息,完全没有任何耦合。 - jlehrbigUIView
需要-touchesBegan:withEvent:
委托方法参数才能正常工作怎么办?覆盖会导致响应链停止,您建议创建另一个事件阶段的“同级”方法签名并在bigUIView
中实现它们(这些方法将由UIViewController
调用)吗? - Eduardo CoelhobigUIView
中每个重写方法的末尾包含对[super touchesBegan:withEvent:]
的调用,这将允许消息继续沿着响应者链传递,以便它们被bigUIView
和其控制器接收。 - jlehr