0

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?

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
evenwerk
  • 947
  • 1
  • 12
  • 28
  • This is already built into both animations. – evenwerk Feb 08 '18 at 08:22
  • 1
    @TimE: I don't know the complete solution, but you should look for the [CAMediaTiming](https://developer.apple.com/documentation/quartzcore/camediatiming) protocol, which is implemented by `CAAnimation` and `CALayer`. With `beginTime` and `timeOffset` you may modify the start point of animations. – clemens Feb 08 '18 at 08:31
  • 1
    These may help : https://stackoverflow.com/questions/2306870/is-there-a-way-to-pause-a-cabasicanimation and https://stackoverflow.com/questions/7568567/restoring-animation-where-it-left-off-when-app-resumes-from-background – Sharad Chauhan Feb 08 '18 at 10:02

0 Answers0