如何接收子视图内的触摸开始事件?
我有一个包含两个子视图(纯 UI 视图)的视图。父视图识别触摸事件,如触摸开始、结束等,但子视图不识别触摸事件。如何让它识别触摸事件。具体来说,我只需要子视图来识别触摸事件,而不需要父视图。提前致谢。
视图的 UserInteractionEnabled 设置为 YES。
I have a view containing two subviews(plain UIviews). The parent view recognizes touch events like touchesbegan ,ended, etc. but the sub view is not recognizing the touch event. How to make it recognize the touch event. Specifically i need only sub view to recognize touch events and not parent view. Thanks in advance.
UserInteractionEnabled is set to YES for the views.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法可以处理这个问题。
1)检测视图控制器中的触摸而不是视图中的触摸。您应该能够找出哪个视图包含被触摸的点,并据此可以决定如何处理触摸(或者如果不在任一视图中,则将其丢弃)。
2) 创建 UIView 的子类并将它们添加到主视图中,而不仅仅是 UIViews。然后您可以检测子类内的触摸并相应地处理它们。
希望有帮助。
There's two ways of dealing with this.
1) Detect the touches in your viewcontroller instead of in a view. You should be able to work out which view contains the point which was touched and from that can decide what to do with the touch (or just throw it away if it wasn't in either view).
2) Create a subclass of UIView and add these to your main view instead of just UIViews. Then you can detect the touches inside your subclass and deal with them accordingly.
Hope that helps.