Can I manage the CAAnimation timeline?

I have a complex animation that usually runs on its own, driven by a specific CAMediaTimingFunction . It works great.

Now I want to control the same animation time (string) using an external value, for example, from a slider or gesture recognizer. In other words, I do not want the “clock” to control the timeline, but a slider, so you can smooth it and “freeze” the animation by setting the slider to a specific value.

Is it possible? If so, how?

+4
source share
1 answer

It is possible (and quite easy), but I just tried it as an experiment (for complex animation controlled by gesture recognition), so I would like to hear if this solution is enough:

You need to set the speed of your animation to 0 and the time offset to the moment you want to go to, for example

 CABasicAnimation* animation = [CABasicAnimation ...]; animation.speed = 0; animation.duration = 1; animation.timeOffset = 0.5; 

make the animation go to a point that will be in half a second.

Now you cannot manipulate CAAnimation objects after adding them to this layer, so you need to add a new animation every time the offset changes (and delete the old one, do not forget;).

0
source

All Articles