收听iOS应用中的所有触摸事件

and*_*lin 21 events objective-c touch ios

有可能以某种方式听取并捕获应用程序中发生的所有触摸事件吗?

我正在开发的应用程序将用于展厅和信息亭,因此如果在给定的几分钟内没有收到任何触摸,我想恢复到应用程序的开始部分.一种屏幕保护功能,如果你愿意的话.我打算通过在后台运行一个计时器来实现这一点,每当应用程序中的某个地方发生触摸事件时,应该重置并重新启动计时器.但是我怎么能听听触摸事件呢?任何想法或建议?

Sul*_*han 34

这真的很容易.

你需要一个子类UIApplication(让我们称之为MyApplication).

您修改您main.m使用它:


return UIApplicationMain(argc, argv, @"MyApplication", @"MyApplicationDelegate");

并重写该方法[MyApplication sendEvent:]:


- (void)sendEvent:(UIEvent*)event {
    //handle the event (you will probably just reset a timer)

    [super sendEvent:event];
}


Dar*_*ust 5

UIWindow通过重写可以使用的子类来实现此目的hitTest:。然后,在主窗口的XIB中,通常有一个简单的对象Window。单击该按钮,然后在“实用程序”窗格的右侧,转到“标识”(Alt-Command-3)。在“ 类”文本字段中,输入UIWindow子类的名称。

MyWindow.h

@interface MyWindow : UIWindow
@end
Run Code Online (Sandbox Code Playgroud)

MyWindow.m

@implementation MyWindow

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

    res = [super hitTest:point withEvent:event];

    // Setup/reset your timer or whatever your want to do.
    // This method will be called for every touch down,
    // but not for subsequent events like swiping/dragging.
    // Still, might be good enough if you want to measure
    // in minutes.

    return res;
}   

@end
Run Code Online (Sandbox Code Playgroud)