CABasicAnimation - 从表示层继续动画,但不从fromValue继续

时间:2018-02-08 08:17:03

标签: ios swift calayer cabasicanimation

我正在制作一个可以在需要时取消的循环动画。取消动画时,将调用另一个动画,将表示层动画回上一个动画的fromValue。

循环动画:

var toBounds = layer.bounds
toBounds.size.height = 12

let animation = CABasicAnimation(keyPath: "bounds")
animation.fromValue = layer.bounds
animation.toValue = toBounds
animation.repeatCount = HUGE
animation.autoreverses = true
animation.duration = 0.3
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
layer.removeAllAnimations()
layer.add(animation, forKey: "oscillation")

取消动画:

if let presentationLayer = layer.presentation() {
    let animation = CABasicAnimation(keyPath: "bounds")
    animation.fromValue = presentationLayer.bounds
    animation.toValue = layer.bounds
    animation.duration = 0.3
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
    layer.removeAllAnimations()
    layer.add(animation, forKey: "decay")
}

这使得动画在返回原始状态时看起来很流畅。但是,当取消动画仍然忙时动画重新启动时,动画会产生这种卡顿效果。我试图做的是在循环动画中更改fromValue,但这意味着动画不会返回到所需的fromValue。如何让这次重启顺利?有没有办法设置动画的当前值?

0 个答案:

没有答案