在UIView中捕捉所有触摸事件? [英] Catch all touch events in UIView?

查看:68
本文介绍了在UIView中捕捉所有触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次用户触摸我的应用程序中的屏幕时都会重置计时器(所以我知道用户仍在使用该设备)。我在我的ViewController中实现了touchBegan。

I want a timer to reset every time the user is touching the screen in my app (So i know that user are still using the device). I have implemented touchBegan in my ViewController.

问题是如果我的ViewController中有一个UITableView并且用户触摸UITableView,则函数touchBegan永远不会被调用。原因是tableView不会传递触摸事件。

The problem is if i have a UITableView in my ViewController and user touches the UITableView the function touchBegan is never being called. Reason is that the tableView dosn't pass along the touch event.

我所知道你可以将UITableView子类化为传递给它的超级视图或者添加透明的UIView最重要的是,只能抓住触摸事件并传递它们。子类化的问题是我需要在所有捕获触摸事件的类上执行此操作。

What i've understand you can either subclass UITableView to pass it to it's superview or add a transparent UIView on top that only catches touch event and passing them. Problem with subclassing is that i need to do it on all classes that catches touch event.

有更好的解决方案吗?如果不是如何将事件传递给它的超级视图?

Is there a better solution? If not how do you pass the event to it's superview?

推荐答案

在我的脑海中,您可以继承 UIWindow UIApplication 并覆盖 sendEvent:(UIEvent *)事件并带走来自那里的行动。

Off the top of my head, you could subclass UIWindow or UIApplication and override sendEvent:(UIEvent *)event and take action from there.

ex (如果你覆盖 UIApplication

e.x. (if you override UIApplication)

- (void)sendEvent:(UIEvent *)event {
   if (event.type == UIEventTypeTouches)
      [(YourApplicationDelegate *)self.delegate wakeUp:event];
   [super sendEvent:event];
}  

这可能对您尝试做的事情有点侵略性。很想看到其他一些想法。

This may be a bit invasive for what you're trying to do. Would love to see some other ideas.

这篇关于在UIView中捕捉所有触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆