ios 中的全局触摸事件

Global Touch Event in ios

我的问题与应用程序中的全局触摸事件有关

这是我的场景,

我有一个有 15 个屏幕的应用程序,当第一个屏幕启动时计时器开始,用户随机从一个屏幕转到另一个屏幕,在这种情况下我需要重置计时器,如果计时器达到(比如 120 秒)我必须销毁当前用户会话并注销 n 从第一个屏幕开始。

startLogoutTimer resetLogoutTimerstopLogoutTimer 的实现代码在 AppDelegate class 中,如果用户导航到任何屏幕并触摸屏幕,我需要执行startLogoutTimer、resetLogoutTimer 和 stopLogoutTimer 的方法基于操作,但没有从每个 class (Screen/ViewController).

调用 AppDelegate 的消息

谢谢。

为 UIView 创建类别并在其中处理触摸。

@implementation UIView (Touch)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [((Appdelegate*)[UIApplication sharedApplication].delegate) resetLogoutTimer];

}

或者你也可以用 swizzling 做同样的事情

我上面的问题已经解决了。 我将以下代码添加到 AppDelegate class

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetLogoutTimer) name:UITouchPhaseBegan object:nil];

现在,无论我触摸屏幕上的什么地方,我的计时器都会重新启动。

嗨,下面两个链接解决了我的问题

iOS perform action after period of inactivity (no user interaction)

Timing Out An Application Due To Inactivity