【发布时间】:2018-08-05 01:34:49
【问题描述】:
我在 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 会触发,即使我只是进行一次长按?
谢谢。
【问题讨论】:
-
函数调用时手势识别器处于什么状态?
-
UILongPressGestureRecognizer 有两种状态,开始和结束。试试这个:
@IBAction func longTap(_ sender: UIGestureRecognizer){ print("Long tap") if sender.state == .ended { print("UIGestureRecognizerStateEnded") //Do Whatever You want on End of Gesture } else if sender.state == .began { print("UIGestureRecognizerStateBegan.") //Do Whatever You want on Began of Gesture } } -
不管它处于什么状态。调用了错误的函数。
标签: ios swift uilongpressgesturerecogni