iOS - 动画未停止,并且未按预期调用完成方法

时间:2016-06-03 10:23:43

标签: ios swift animation completion

我很难停止动画,并阻止调用动画的完成功能并返回一个TRUE变量。

以下代码是我的代码的简化版本,存在相同的问题。

override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    viewButtonStart.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
    viewButtonStart.backgroundColor = UIColor.greenColor()
    let gesture = UITapGestureRecognizer(target: self, action: #selector(startAnimation))
    viewButtonStart.addGestureRecognizer(gesture)
    addSubview(viewButtonStart)

    viewButtonStop.frame = CGRect(x: 120, y: 10, width: 100, height: 100)
    viewButtonStop.backgroundColor = UIColor.yellowColor()
    let gesture2 = UITapGestureRecognizer(target: self, action: #selector(stopAnimation))
    viewButtonStop.addGestureRecognizer(gesture2)
    addSubview(viewButtonStop)

    viewShow.backgroundColor = UIColor.blueColor()
    viewShow.frame = CGRect(x: screenWidth - 10 - 100, y: 10, width: 100, height: 100)
    addSubview(viewShow)

    isAnimationRunning = false

    startAnimation()
    startAnimation()
    startAnimation()
}

func startAnimation()
{
    print("startAnimation()")

    if isAnimationRunning
    {
        stopAnimation()
    }

    // (*1) More data handling here...

    isAnimationRunning = true
    UIView.animateKeyframesWithDuration(
        5,
        delay: 0,
        options: UIViewKeyframeAnimationOptions.CalculationModeLinear,
        animations: animations(),
        completion: completion())
}

func stopAnimation()
{
    self.viewShow.layer.removeAllAnimations()
}

func animations() -> (() -> Void)
{
    return {
        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1, animations: {
            self.viewShow.alpha = 1
        })
    }
}

func completion() -> ((Bool) -> Void)?
{
    return { (completed: Bool) in
        print("animation completed(\(completed))")
        if completed
        {
            self.isAnimationRunning = false

            // (*2) More completion handling here....
        }
    }
}

每次动画开始时,如果动画正在运行,它将检查并停止。当我在init函数中调用startAnimation()3次时,前2个动画应该停止,但它不会。即使最糟糕的是,动画结束时也不会立即调用完成功能。仅当UIView.addKeyframeWithRelativeStartTime中的属性未更改时才会出现此问题。在示例中几次调用startAnimations()可能看起来除非,但在实际情况下,我将根据不同情况设置并重新启动不同的动画。

控制台中显示的实际结果如下:

startAnimation()
startAnimation()
startAnimation()
animation completed(true)
animation completed(true)
animation completed(true)

我希望有类似的东西:

startAnimation()
startAnimation()
animation completed(false)
startAnimation()
animation completed(false)
animation completed(true)

以下结果也适用于我:

startAnimation()
animation completed(true)
startAnimation()
animation completed(true)
startAnimation()
animation completed(true)

or this:

startAnimation()
startAnimation()
startAnimation()
animation completed(false)
animation completed(false)
animation completed(true)

问题导致变量(* 1)和& (* 2)不正确。

感谢任何帮助或建议,谢谢

1 个答案:

答案 0 :(得分:0)

最后,我通过检查当前属性是否与动画结束属性相同来解决问题。如果属性没有改变,它将跳过调用动画。

这是一个非常脏的方法,但它解决了我的情况。如果您有任何问题,我期待更好的解决方案,谢谢。

相关问题