简体   繁体   English

检测应用程序中的所有触摸

[英]Detecting all touches in an app

In an iPad app, wherever a user is touching the screen I want to display an image, highlighting the points they are touching. 在iPad应用程序中,无论用户何时触摸屏幕,我都想显示图像,突出显示他们触摸的点。 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. touches began: with event使用touches began: with event根视图控制器上的touches began: with event和相关功能无法使用,因为如果触摸了子视图,则不会触发事件。 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. 效果很好,按钮也可以,但是破坏了UIScrollViews,我猜想其他带有手势调整器的子视图。

Is there nowhere you can just access all touch events without affecting where those touches are headed? 是否有无处你可以访问,而不会影响其中的触摸被户主所有触摸事件?

thanks. 谢谢。

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. 为了摆脱其他手势识别器的问题,在此委托方法中返回YES。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

Other option is to subclass main UIWindow in your app and override this method 另一个选择是在您的应用程序中子类化主UIWindow并重写此方法

- (void)sendEvent:(UIEvent *)event

Here you should have access to all events. 在这里,您应该可以访问所有事件。 It's quite easy to filter them. 过滤它们很容易。

You can apply a UITapGestureRecognizer to the entire view and set the cancelsTouchesInView property to NO . 您可以将UITapGestureRecognizer应用于整个视图,并将cancelsTouchesInView属性设置为NO 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 like UIScrollView . 此外,您可以实现-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:委托方法,以防止该手势识别器踩踏UIScrollView之类的视图使用的手势识别器。

you can try overriding hitTest:withEvent: 您可以尝试覆盖hitTest:withEvent:

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event

This maybe what you are looking for. 这也许就是您想要的。

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

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