防止UIButton长按重复功能
Prevent UIButton Long Press Repeating Function
以下 UILongPressGestureRecognizer 代码有效,但它会在按住按钮时重复 运行s 长按功能。当用户按下时,我只想 运行 一次。这可能吗?
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tap))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
tapGesture.numberOfTapsRequired = 1
shiftBtn.addGestureRecognizer(tapGesture)
shiftBtn.addGestureRecognizer(longGesture)
}
@objc func tap() {
print("Short Tap")
}
@objc func long() {
print("Long press")
}
谢谢!
您可以在long
函数中查看手势状态。例如检查状态是开始长按
您应该调用 long
当 gesture
激活。只要 long gesture
活跃,它就会被调用一次。
@objc func hitlongprass(_ sender : Any){
guard let longPress = sender as? UILongPressGestureRecognizer else
{ return }
if longPress.state == .began { // When gesture activated
long()
}
else if longPress.state == .changed { // gesture Calls multiple times by time changed
}
else if longPress.state == .ended { // When gesture end
}
}
func long() {
print("Long press")
}
通话中:
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(hitlongprass(_:)))
以下 UILongPressGestureRecognizer 代码有效,但它会在按住按钮时重复 运行s 长按功能。当用户按下时,我只想 运行 一次。这可能吗?
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tap))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
tapGesture.numberOfTapsRequired = 1
shiftBtn.addGestureRecognizer(tapGesture)
shiftBtn.addGestureRecognizer(longGesture)
}
@objc func tap() {
print("Short Tap")
}
@objc func long() {
print("Long press")
}
谢谢!
您可以在long
函数中查看手势状态。例如检查状态是开始长按
您应该调用 long
当 gesture
激活。只要 long gesture
活跃,它就会被调用一次。
@objc func hitlongprass(_ sender : Any){
guard let longPress = sender as? UILongPressGestureRecognizer else
{ return }
if longPress.state == .began { // When gesture activated
long()
}
else if longPress.state == .changed { // gesture Calls multiple times by time changed
}
else if longPress.state == .ended { // When gesture end
}
}
func long() {
print("Long press")
}
通话中:
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(hitlongprass(_:)))