我正在尋找一種方法來檢測用戶是否在屏幕上(屏幕持續1秒)我嘗試使用計時器,但我沒有工作。這是我現在的代碼。Swift 2.0如何檢測用戶是否持有,而不是僅僅點擊
var brakeTimer = NSTimer()
func update() {
print("The user is holding the screen")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
var brakeTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
brakeTimer.invalidate()
}
我認爲你正在尋找一個[UILongPressGestureRecognizer(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILongPressGestureRecognizer_Class/) – Cristik