如何检测自上次触摸iOS中的特定视图控制器以来的用户不活动?

我需要在视图控制器中执行特定的操作,当用户不触摸特定的屏幕3秒钟。

我怎么能在iOS中做到这一点?

覆盖视图的触摸开始方法,并在触摸时重置计时器。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.timer invalidate]; self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(inactivityTimerFired) userInfo:nil repeats:NO]; [super touchesBegan:touches withEvent:event]; } 

如果需要的话,你可以定位视图控制器而不是self

  1. NSTimer属性/ ivar添加到您的视图控制器
  2. 将轻击手势识别器添加到要监视的视图(或使用touchesBegan:
  3. 当轻击识别器被击中时,以3.0秒的间隔启动定时器来调用一个函数
  4. 如果在定时器调用函数之前敲击敲击识别器,则使定时器无效并将其设置nil
  5. 如果定时器正在调用的function被击中,则用户已经停用3秒