I am working on a looping animation that can be cancelled when desired. When the animation is cancelled another animation is called to animate the Presentation Layer back to the fromValue of the previous animation.
The looping animation:
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")
The cancel animation:
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")
}
This makes the animation look smooth when returning to it's original state. However, when the animation gets restarted while the cancel animation is still busy, the animation has this stuttering effect. What I tried to do is change the fromValue in the looping animation but this means that the animation doesn't return to the desired fromValue. How can I make this restart smooth again? Is there a way to set the animation's current value?