简体   繁体   English

如何在自定义UIView中检测屏幕上的触摸?

[英]How to detect touch on screen in custom UIView?

I want to watch in my UIView subclass for user touches on the screen. 我想在UIView子类中查看屏幕上的用户触摸。 Especially there are some additional conditions: 特别是还有一些其他条件:

  • the touch should not be catch by my view (for example, if user taps some button or scrolls something that action should be accepted) 触摸不应被我的视图抓住(例如,如果用户点击某个按钮或滚动某个应接受操作的内容)
  • there can be multiple views which are watching for touch and they should not conflict with each other 可能有多个正在观察触摸的视图,它们不应彼此冲突
  • the approach should be as general as possible (so we can catch taps on tabbar or navigation buttons) 该方法应尽可能通用(这样我们就可以在标签栏或导航按钮上捕捉点击)

I can suppose to add specific fullscreen view with customized hitTest: method. 我可以假设使用自定义的hitTest:方法添加特定的全屏视图。 This view has interface to add/remove delegates for watching for touches. 该视图具有添加/删除代表以观察触摸的界面。 But this method is "hard" in coding terms and I would like to find more elegant and simpler solution. 但是这种方法在编码方面是“困难的”,我想找到更优雅,更简单的解决方案。

you can write touchesBegan in that UIView subclass. 您可以在该UIView子类中编写touchesBegan。

Because if it had any subview like button or scrollView, they would be first responders ,and touches on scrollView wont fire touchesBegan of your UIView subclass. 因为如果它具有诸如button或scrollView的任何子视图,则它们将是第一响应者,并且对scrollView的触摸不会触发UIView子类的touchesBegan。

And it wont conflict with other touch listeners unless the listener subView is explicitly passing that touch to your Subclass. 并且它不会与其他触摸侦听器发生冲突,除非侦听器subView将该触摸显式传递给您的子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM