我在 UIImageView 上添加了一个 UILongPressGestureRecognizer。它的目的是如果用户按下图像动画应该开始。这是完美的。但是一旦用户稍微移动他的手指,动画就会停止。我知道发生这种情况是因为状态发生了变化。但是我想继续动画,只有当用户从图像上移开他的手指时才停止。
到目前为止,我添加手势识别器的代码是
let tapGestureRecognizer = UILongPressGestureRecognizer(target:self, action:Selector("imageTapped:"))
tapGestureRecognizer.minimumPressDuration = 0
barcodeImage.userInteractionEnabled = true
barcodeImage.addGestureRecognizer(tapGestureRecognizer)
动画的代码是
func imageTapped(gesture: UILongPressGestureRecognizer)
{
let animation : CATransition = CATransition()
animation.delegate = self
animation.duration = 2.5
animation.repeatCount = Float.infinity
animation.type = "rippleEffect"
if(gesture.state == UIGestureRecognizerState.Ended){
logger.log("press ended");
// and stop animation
barcodeImage.layer.removeAllAnimations()
}else{
logger.log("show animation")
barcodeImage.layer.addAnimation(animation, forKey: nil)
}
}
当状态改变时如何继续动画?
最佳答案
barcodeImage.userInteractionEnabled = true
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (touches.first?.view == barcodeImage) {
print("show animation")
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("removeAllAnimations")
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
print("removeAllAnimations")
}
关于ios - UILongPressGestureRecognizer 在状态改变时继续动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430192/