ios - UILongPressGestureRecognizer 在状态改变时继续动画

标签 ios swift animation

我在 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/

相关文章:

ios - 断言失败 : (result == KERN_SUCCESS), 函数 +[XPCMachSendRight wrapSendRight:]

ios - 如何根据传入远程通知负载中定义的类别添加不同的操作? swift 更新

javascript - 在音频播放上移动图像

ios - 如何更新 WDA WebDriverAgentRunner 产品构建版本?

iphone - 将多个 pdf 文档合并为单个文档不起作用

ios - 如何在 Swift 中让所有页面共用菜单

ios - 如何使用for循环在页面控件中打印数组(swift4)

javascript - 如何禁用 Sencha Touch 2 页面转换动画?

javascript - 让 jquery 添加(或触发?)动画

ios - UIImagePickerControllerEditedImage 得到零