传递触摸事件
我的视图控制器视图中有一个正在工作的 UIButton。
我在同一视图控制器的视图中的按钮顶部有一个 UIView 实例(我将其称为 topView)。如果满足条件,我会将所有触摸事件(开始、移动、结束、取消)转发到视图的 nextResponder。
但是当条件被触发时,按钮没有接收到事件。如果我移动 topView 使其不在按钮上方,则按钮将起作用(或将 userInteractionEnabled 属性设置为 false)。如果 topView 位于按钮上方,则该按钮不起作用。太令人沮丧了,因为我可以看到视图下方的按钮!
我是否需要在视图控制器中执行任何操作才能接收触摸事件并将其发送到 nextResponder?
我已经阅读了响应者链上的所有文档,但我仍然有点困惑。
I have a working UIButton in my view controller's view.
I have a UIView instance on top of the button (I'll refer to it as topView) in the same view controller's view. If a condition is met I am forwarding all of the touch events (began, moved, ended, canceled) to the view's nextResponder.
But when the condition is triggered, the button is not receiving the event. If I move the topView so it is not over the button, the button works (or set the userInteractionEnabled property to false). If topView is over the button, the button does not work. So frustrating because I can see the button under the view!
Do I have to do anything in the view controller to receive the touch events from sending them to the nextResponder?
I have read all the documents I can on the Responder Chain, but I am still a bit confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
视图的 nextResponder 要么是它的视图控制器,要么是它的超级视图。您设置中的 UIButton 两者都不是,因此它不会接收事件。
在这种情况下,您应该覆盖
pointInside:withEvent:
以返回 NO,而不是将事件转发到 nextResponder。这样系统就会表现得好像您的视图根本不存在一样。根据文档,您还可以在视图上将 userInteractionEnabled 设置为 NO 以获得相同的效果。The nextResponder of a view is either its view controller or its superview. The UIButton in your setup is neither, so it doesn't receive the events.
In this situation, instead of forwarding the events to the nextResponder you should override
pointInside:withEvent:
to return NO. That way the system will act like your view isn't even there. According to the documentation, you could also set userInteractionEnabled to NO on your view for the same effect.