I added an CAKeyframeAnimation with the Key "SpeedBoat" to a CALayer. Now I am trying to change the speed of the animation during the animation.
[[self.shipLayer animationForKey:@"SpeedBoat"] setSpeed:([[self.shipLayer animationForKey:@"SpeedBoat"] speed] + deltaTime)];
There seems to be an error in this line. But I do not get any debug informations.
What is wrong here? Is it possible to change the speed of an ongoing animation?
It would be very nice if somebody could help me :)
Best regards
xen
Yes, you can definitely change the speed of an animation. That's because CAKeyframeAnimation
confirms to the protocol CAMediaTiming
.
Apple explains this in their Animation Types and Timing Programming Guide.
Have a look at the section on Timing, Timespaces, and CAAnimation.
Specifically:
"The speed property of an animation or layer specifies this scaling factor. For example, a 10 second animation that is attached to a layer with a timespace that has a speed value of 2 will take 5 seconds to display (twice the speed). If a sublayer of that layer also defines a speed factor of 2, then its animations will display in 1/4 the time (the speed of the superlayer * the speed of the sublayer)."
Could I also point you in the direction of some excellent videos that cover this? Have a look at:
WWDC 2010 Sessions 424 and 425 Core Animation in Practice Parts 1 and 2
WWDC 2011 Session 421 Core Animation Essentials
and
Developer Videos Session 716 Core Animation Techniques for iPhone and Mac
You can even change the speed of an animation to effectively "freeze" it. It's very useful for pausing and resuming. Although one caveat is that it won't behave properly if the app is backgrounded, or purged from memory. That you'll have to deal with manually.
See the Technical Q&A QA1673, and my answer to this question for more info on that process:
Is there an issue with updating a CALayer position while the layer is paused?