views:

1569

answers:

3

I want to do some sophisticated animations. But I only know how to animate a block of changes. Is there a way to chain animations, so that I could for example make changes A -> B -> C -> D -> E -> F (and so on), while the core-animation waits until each animation has finished and then proceeds to the next one?

In particular, I want to do this: I have an UIImageView that shows a cube.

Phase 1) Cube is flat on the floor

Phase 2) Cube rotates slightly to left, while the rotation origin is in bottom left.

Phase 3) Cube rotates slightly to right, while the rotation origin is in the bottom right.

These phases repeat 10 times, and stop at Phase 1). Also, the wiggling becomes lesser with time.

I know how to animate ONE change in a block, but how could I do such a repeating-thing with some sophisticated code in between? It's not the same thing over time. It changes, since the wiggling becomes lesser and lesser until it stops.

A: 

I think the answer to your question is that you need to get more specification on what you want to do. You clearly have an idea in mind, but more specification of details will help you answer your own question.

McWafflestix
Thanks. I added some details.
Thanks
+1  A: 

You'll need to setup an animation delegate inside of your animation blocks. See setAnimationDelegate in the UIView documentation for all the details.

In essence your delegate can be notified whenever an animation ends. Use the context parameter to determine what step in your animation is currently ending (Phase 1, Phase 2, etc)

This strategy should allow you to chain together as many animation blocks as you want.

Jonathan Arbogast
Thanks for that context hint! Helped a lot!
Thanks
+2  A: 

Assuming you're using UIView animation...

You can provide an animation 'stopped' method that gets called when an animation is actually finished (check out setAnimationDelegate). So you can have an array of animations queued up and ready to go. Set the delegate then kick off the first animation. When it ends it calls the setAnimationDidStopSelector method (making sure to check the finished flag to make sure it's really done and not just paused). You can also pass along an animation context which could be the index in your queue of animations. It can also be a data structure that contains counters so you can adjust the animation values through each cycle.

So then it's just a matter of waiting for one to be done then kicking off the next one taken off the queue.

The same method applies if you're using other types of animation. You just need to set up your data structures so it knows what to do after each one ends.

Ramin