How to detect user inactivity since last touch on a particular viewcontroller in iOS?

半城伤御伤魂 提交于 2019-12-17 21:09:03

问题


I need to perform a particular action in a view controller when user don't touch a particular screen for 3 seconds.

How can I do that in iOS?


回答1:


Override the view's touches began method and reset a timer when you get touches.

- (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];
}

You could target the view controller instead of self if you need to.




回答2:


  1. Add an NSTimer property/ivar to your view controller
  2. Add a tap gesture recognizer to the view you want to monitor (or use touchesBegan:)
  3. When the tap recognizer is hit, start the timer with an interval of 3.0s to call a function
  4. If tap recognizer is hit before the function the timer calls, invalidate the timer and set it to nil
  5. If the function that the timer was calling is hit, the user has been inactive for 3 seconds


来源:https://stackoverflow.com/questions/26001574/how-to-detect-user-inactivity-since-last-touch-on-a-particular-viewcontroller-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!