我在 IB 中创建了两个长按手势识别器,并为它们创建了两个 IBAction。
@IBAction func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do long press")
}
@IBAction func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do something else")
}
longPressGesture 设置为 > 0.5,0 次点击,1 次触摸。
longPressTapGesture 设置为 > 0.5,1 次点击,1 次触摸。
所以从技术上讲,当我启动应用程序并按下屏幕上的任意位置时,我应该触发 longPressGesture。
相反,在我开始运行该应用程序后的第一次,它总是触发 longPressTapGesture,无论我使用什么手势。
如果我抬起手指,然后再次按下,这次会触发 longPressGesture。
为什么 longPressTapGesture 会触发,即使我只是长按一次,有什么建议吗?
谢谢。
最佳答案
@Bevan,根据我所做的测试,您没有做错任何事。通过 Storyboard添加手势识别器时似乎出现了问题。当我使用 Storyboard构建测试应用程序并设置您在上面描述的设置时,我看到了您所看到的确切问题。但是,当我在代码中创建相同的设置时,行为会如您所期望的那样起作用。对于这种情况,最好的选择可能是使用代码而不是 Storyboard。
下面的代码示例:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tap = UILongPressGestureRecognizer(target: self, action: #selector(longPressTapGesture))
tap.minimumPressDuration = 0.5
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
view.addGestureRecognizer(tap)
let press = UILongPressGestureRecognizer(target: self, action: #selector(longPressGesture))
press.minimumPressDuration = 0.5
press.numberOfTouchesRequired = 1
view.addGestureRecognizer(press)
}
@objc func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do long press")
}
@objc func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
print("Do something else")
}
}
关于ios - 错误的 UILongPressGestureRecognizer IBAction 被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974212/