如何判斷 user 是否 idle timeout, 就是在 on touch 裡 reset 掉 timer counter 應該就可以了。
Decalare a timer
var timer = NSTimer()
In your viewDidLoad
do set the timer to whatever you like
Here terminateApp
will be called after 60 seconds
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: Selector("terminateApp"), userInfo: nil, repeats: true)
func terminateApp(){
// Do your segue and invalidate the timer
timer.invalidate()
}
But if the user presses the view you want to invalidate the timer and start a new timer, do that by:
In your viewDidLoad
add a gestureRecognizer
that will call resetTimer when the user touches the screen:
let resetTimer = UITapGestureRecognizer(target: self, action: "resetTimer");
self.view.userInteractionEnabled = true
self.view.addGestureRecognizer(resetTimer)
func resetTimer(){
// invaldidate the current timer and start a new one
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: Selector("terminateApp"), userInfo: nil, repeats: true)
}
另一個解法:
My take is that you need to catch the event when user touches a screen. Method: touchesBegan:withEvent
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// get the time measurement
}
Implementing UITapGestureRecognizer
for all kind of views will be a workaround solution try with that.
override func viewDidLoad(){
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: view, action: "handleSingleTap:")
tapRecognizer.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapRecognizer)
}
func handleSingleTap(recognizer: UITapGestureRecognizer) {
//Do something here with the gesture
}
For touch began and touch end try with that code by overriding native methods.
override func touchesBegan(touches: Set, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
let touch: UITouch = touches.first as! UITouch
if (touch.view == yourView){
println("touchesBegan | This is an ImageView")
}else{
println("touchesBegan | This is not an ImageView")
}
}
touchend func.
override func touchesEnded(touches: Set, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
let touch: UITouch = touches.first as! UITouch
if (touch.view == yourView){
printing("touchesEnded | This is an ImageView")
}
else{
println("touchesEnded | This is not an ImageView")
}
}