On one of my projects I discovered a bug in a never-ending animation I had set up. Whenever the app was suspended (such as when you multitask and open another app), on relaunching the app the animation was frozen. After some investigating, I discovered that with Core Animation you need to set a flag on the CABasicAnimation class called removedOnCompletion to NO otherwise the animation will get cleaned up when it gets suspended.
CABasicAnimation *dartWiggleAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; dartWiggleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x-DART_WIGGLE_HALF, self.center.y+DART_CENTER_H_OFFSET)]; dartWiggleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x+DART_WIGGLE_HALF, self.center.y+DART_CENTER_H_OFFSET)]; dartWiggleAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; dartWiggleAnimation.duration = 0.4; dartWiggleAnimation.repeatCount = HUGE_VALF; dartWiggleAnimation.autoreverses = YES; dartWiggleAnimation.removedOnCompletion = NO; [[nextDart layer] addAnimation:dartWiggleAnimation forKey:@"dartWiggle"];
let dartWiggleAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.position)) dartWiggleAnimation.fromValue = CGPoint(x: self.center.x - DART_WIGGLE_HALF, y: self.center.y + DART_CENTER_H_OFFSET) dartWiggleAnimation.toValue = CGPoint(x: self.center.x + DART_WIGGLE_HALF, y: self.center.y + DART_CENTER_H_OFFSET) dartWiggleAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut) dartWiggleAnimation.duration = 0.4 dartWiggleAnimation.repeatCount = Float.greatestFiniteMagnitude dartWiggleAnimation.autoreverses = true dartWiggleAnimation.isRemovedOnCompletion = false nextDart.layer.add(dartWiggleAnimation, forKey: "dartWiggle")
Does this seem like a bug or a feature?
It seems like a bug. Thanks a lot for a tip – I was looking for solution of this problem for ages!
Glad I could help Tony. If I get time I might post this to Apple’s bug reporter.
Thank you so much, every other answer I’ve seen for this problem has been totally ridiculous. This is such a simple fix.