UIPanGestureRecognizer停留在状态开始
问题描述:
我有一个我想拖动的SpringButton。这是我的代码:UIPanGestureRecognizer停留在状态开始
var gesture = UIPanGestureRecognizer(target: self, action: #selector(userDragged))
card.addGestureRecognizer(gesture)
var cardIsCurrentlyMoving = false
func userDragged(gesture: UIPanGestureRecognizer){
if !cardIsCurrentlyMoving{
if let button = gesture.view as? SpringButton {
if gesture.state == .began {
print("begon")
cardIsCurrentlyMoving = true
startPosition = button.center
} else if gesture.state == .changed {
print("changed")
} else if gesture.state == .ended{
print("ended")
cardIsCurrentlyMoving = false
}
}
}
}
函数被调用,状态变为.began
。但是,当试图移动按钮时,什么都不会发生。这是因为在.began
中设置为true,但从不回到false,因为gesture.state .changed
和.ended
从不被调用。
当我松开手指并再次触摸按钮时,没有任何反应。为什么UIPanGestureRecognizer
不执行.changed
和.ended
?
谢谢。
答
我认为你需要检查你的if语句
if !cardIsCurrentlyMoving{
}
平移手势不断调用方法与改变的状态。所以userDragged函数不断调用,但仅仅因为你的上面的if语句,控制不会进入if语句中。
所以我建议使用这个,而不是你的。
func userDragged(gesture: UIPanGestureRecognizer){
if let button = gesture.view as? SpringButton {
if gesture.state == .began {
print("begon")
cardIsCurrentlyMoving = true
startPosition = button.center
} else if gesture.state == .changed {
print("changed")
} else if gesture.state == .ended{
print("ended")
cardIsCurrentlyMoving = false
}
}
}
O这是愚蠢的从我嘿嘿...会在4分钟内接受答案 –
这种情况有时与我同时发生:) – Surjeet