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

前端 未结 2 831
渐次进展
渐次进展 2020-12-11 23:08

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?

2条回答
  •  自闭症患者
    2020-12-11 23:23

    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.

提交回复
热议问题