How to detect user inactivity since the last touch of a specific view manager in iOS?
2 answers
Override the initial view method and reset the timer when you receive 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 can target the view controller instead self
if you need to.
+3
source to share
- Add the
NSTimer
/ ivar property to the view controller - Add a gesture recognizer to the view you want to control (or use
touchesBegan:
) - When the tap recognizer is triggered, start the interval timer
3.0
to trigger the function - If the recognize function is discarded before the function called by the timer, cancels the timer and sets it to
nil
- If the function called by the timer hits, the user is inactive for 3 seconds.
+1
source to share