2

我遇到了停止动画的困难,并阻止调用动画的完成函数并返回一个 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) 中使用的变量不正确。

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

4

1 回答 1

0

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

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

于 2016-06-06T08:44:47.723 回答