iPhone:透明地处理触摸
我有一个 UIViewController 和一个与之关联的 UIView。 UIView 有许多子视图。
如何处理 UIViewController 子类中指定矩形内的触摸事件?
这种处理应该是透明的:UIView 及其子视图仍然应该接收其触摸事件,即使它们与指定矩形相交。
PS
- 覆盖触摸在视图中开始 控制器不工作。内心观点 不传递事件。
- 添加自定义 按钮到 UIView 子视图的末尾 列表也不起作用。内 子视图没有收到触摸 事件。
I have a UIViewController and an associated with it UIView. The UIView has a number of subviews.
How to handle touch event inside a specified rectangle in my UIViewController subclass?
This handling should be transparent: UIView and its subviews should still receive their touch events even if they intersect with the specified rectangle.
P.S.
- Overriding touchesBegan in the view
controller doesn't work. Inner views
doesn't pass events through. - Adding a custom
button to the end of UIView subviews
list also doesn't work. Inner
subviews doesn't receive touch
events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TouchesBegan/touchesMoved/touchesEnded 可能是要走的路。根据您想要执行的操作,UIGestureRecognizer 也可能是一个选择。
要确保子视图向上传递事件,请将子视图上的 userInteractionEnabled 设置为 YES。
UIView 默认情况下将其设置为 YES,但某些子类(特别是 UILabel)会覆盖此设置并将其设置为 NO。
touchesBegan/touchesMoved/touchesEnded is probably the way to go. Depending on exactly what you are trying to do, UIGestureRecognizer may also be an option.
To make sure subviews pass events up, set userInteractionEnabled to YES on the subviews.
UIView sets this to YES by default, but some subclasses (notably UILabel) override this and set it to NO.
仅仅因为视图默认情况下不传递事件并不一定意味着它必须如此。
您可以对正在使用的视图进行子类化,并让它们将事件传递到视图控制器上。
另一个想法是在所有视图之上有一个透明的处理程序。也就是说,有一个透明的 UIView 位于视图上方,处理触摸事件并传递它们。我不知道这在实践中是否有效,但听起来确实如此。
Just because the views don't pass events through by default doesn't necessarily mean that's the way it has to be.
You could subclass the views that you are using and have them pass the events onto the view controller.
Another idea is to literally have a transparent handler on top of all of your views. That is, have a transparent UIView that sits above your views, handling touch events but also passing them through. I have no idea if this works in practice, but it sounds like it would.
您的视图及其控制器可以使用touchesBegan、touchesEnded 或touchesMoved 来处理触摸。以 TouchsBegan 为例,您可以选择将事件传递给
响应者链中的下一个响应者。这样,您的每个视图都有机会根据触摸事件执行某些操作。
Your views, and their controllers can handle a touch using touchesBegan, touchesEnded or touchesMoved. Within touchesBegan as an example you can then choose to pass the event to
the next responder in the responder chain. This way each of your views will get a chance to do something based on the touch event.