我正在寻找一种方法来检测用户是否按住屏幕(按住屏幕1秒)我尝试使用计时器,但我没有工作。这是我现在的代码。
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()
}
答案 0 :(得分:6)
&#34;保持&#34;在iOS开发中通常称为界面&#34; Long Pressing&#34;。以下是如何设置其中一个:
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)
func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
}
与简单的点击识别器相反,当用户只是点击屏幕而不是保持更长的时间时,它会触发。
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
self.view.addGestureRecognizer(tapGestureRecognizer)
func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
}