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

前端 未结 2 832
渐次进展
渐次进展 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

    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

提交回复
热议问题