检测应用程序中的所有触摸
在 iPad 应用程序中,无论用户触摸屏幕,我都希望显示图像,突出显示他们触摸的点。该应用程序包含许多嵌套视图,所有这些视图都应该接收触摸并正常运行。
看起来很简单,但我没能找到一个好的方法来做到这一点。在根视图控制器上使用 touches begin: with event
和相关函数不起作用,因为如果触摸子视图,则不会触发事件。我还创建了一个“虚拟”手势识别器,它只是将触摸事件传递给另一个绘制图像的类。这工作得很好,按钮也能工作,但会破坏 UIScrollViews,我猜还有其他带有手势识别器的子视图。
是否有地方可以访问所有触摸事件而不影响这些触摸的去向?
谢谢。
In an iPad app, wherever a user is touching the screen I want to display an image, highlighting the points they are touching. The app contains a number of nested views, all of which should receive touches and behave normally.
Seems simple, but I've failed to find a good way to do it. Using the touches began: with event
and related functions on the root view controller does not work because if a subview is touched the events are not fired. I've also created a 'dummy' gesture recognizer which just passes touch events to another class which draws images. That works great-ish and buttons work, but breaks UIScrollViews and I'm guessing other subviews with gesture reconizers.
Is there nowhere you can just access all touch events without affecting where those touches are headed?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的虚拟手势识别器应该没问题。只需注意设置状态即可。可能->开始-> ...
基本上,您的手势识别器会转发所有触摸,因此在任何触摸存在时它都可以始终处于开始或可能状态。
要解决其他手势识别器的问题,请在此委托方法中返回 YES。
其他选项是在应用程序中子类化主 UIWindow 并重写此方法
在这里您应该有权访问所有事件。过滤它们非常容易。
Your dummy gesture recognizer should be ok. Just watch out for setting states. possible -> began -> ...
Basically your gesture recognizer is forwarding all touches so it can be in began or possible state all the time while any touch exists.
To get rid of problems with other gesture recognizers return YES in this delegate method.
Other option is to subclass main UIWindow in your app and override this method
Here you should have access to all events. It's quite easy to filter them.
您可以将
UITapGestureRecognizer
应用于整个视图,并将cancelsTouchesInView
属性设置为NO
。这将允许您收到视图及其子视图上的所有点击的通知,而无需拦截所有触摸事件。此外,您可以实现 -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 委托方法,以防止此手势识别器踩踏
UIScrollView
等视图使用的手势识别器。You can apply a
UITapGestureRecognizer
to the entire view and set thecancelsTouchesInView
property toNO
. This will allow you to be notified of all taps on the view and its subviews without intercepting all of the touch events.Additionally, you can implement the
-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
delegate method to keep this gesture recognizer from stomping on the ones used by views likeUIScrollView
.您可以尝试覆盖 hitTest:withEvent:
这可能是您正在寻找的。
you can try overriding hitTest:withEvent:
This maybe what you are looking for.