Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JGoose

macrumors member
Original poster
Feb 12, 2010
41
0
Does anyone know where I can find a detailed explanation of how to make a chain of animation blocks? I googled it but couldn't really find anything.

What I want to do is move an image, then fade it out after it has moved.

Thank you.
 
My Core Animation is fuzzy since I only do it every once in a while, but how about creating a CAAnimationGroup with a CABasicAnimation that does the move, and another CABasicAnimation that does the fade. Then set the beginTime of each animation so they follow each other?
 
I am just trying to learn this for the first time so a more detailed explanation would be helpful.

I've looked at the documentation and I just can't make sense of how to make an animation chain.
 
I'd suggest studying the coding how-tos and sample code, there's a lot of good info there:
http://developer.apple.com/iphone/index.action

Anyways, here's a quick and dirty demo of how to do what you want, using the UIView methods which makes it really easy to animate views.
 

Attachments

  • AnimationTest.zip
    20.2 KB · Views: 239
You just set the animationDidStopSelector to a method that starts a second animation after the first one is done.
 
How about just

HTML:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: AnimationTime];
Image.center = CGPointMake(X axis, Y axis);
Image.alpha = 0.00;
[UIView commitAnimations];
 
How about just

HTML:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: AnimationTime];
Image.center = CGPointMake(X axis, Y axis);
Image.alpha = 0.00;
[UIView commitAnimations];
Because that will fade it out at the same time as moving it. The OP wants a two-phase animation: movement then fading.
 
I'd suggest studying the coding how-tos and sample code, there's a lot of good info there:
http://developer.apple.com/iphone/index.action

Anyways, here's a quick and dirty demo of how to do what you want, using the UIView methods which makes it really easy to animate views.

I updated this demo to take advantage of blocks in iOS 4+ and figured I would post it to help future users. All the original code has been commented out to keep continuity.
 

Attachments

  • AnimationTest.zip
    40 KB · Views: 114
Last edited:
Does anyone know where I can find a detailed explanation of how to make a chain of animation blocks? I googled it but couldn't really find anything.

What I want to do is move an image, then fade it out after it has moved.

Thank you.

There are lots of ways you can handle this.

The simplest is to use the UIView animation method "animateWithDuration:delay:eek:ptions:animations:completion:" Simply pass in a zero delay for the first step, and a delay for the second step that starts after the first step is done, and the third step with a delay that's the total time for the first 2 steps, etc, etc.

The second way is to invoke the first animation and pass the second animation step in as the completion block for the first step, with the third step embedded as the completion for the second step. Those nested blocks get nasty around the 3rd level of nesting however.

The remaining ways involve creating CAAnimation objects, which act on layers, not views. (There is a whole new level of complexity (and really weak documentation) to get through in order to use that approach however. If you can make your animation work with view-based animations, do that. It's much simpler.)

If all your animations act on the same layer, you could create a CAAnimationGroup where the first animation has a beginTime of zero, the second has a beginTime set to the duration of the first one, etc. (Much like the view-based animations with staggered delay values described above.)

If your animations apply to different layers, you can use the CAMediaTiming protocol, which various Core Animation objects conform to, to set the begin time of each animation independently. The tricky part here is that when animations are not part of an animation group, "now" is CACurrentMediaTime(), and delays are an offset from CACurrentMediaTime()

So your begin times would look like this:
animation 1 beginTime = CACurrentMediaTime()
animation 2 beginTime = CACurrentMediaTime() + animation 1 duration
animation 3 beginTime = CACurrentMediaTime() + total duration of previous
animation 4 beginTime = CACurrentMediaTime() + total duration of previous...

A third way to sequence CAAnimation objects is to chain them together with the completion method (CAAnimation objects have an optional delegate, and will send that delegate a animationDidStop:finished: method once the animation is complete.) That gets ugly, however, because all animations use the same selector so you can't write different completion methods for each animation very easily. The completion method gets the animation that completed as a parameter, but the system copies your animation object when you submit it, so you can't compare it directly in order to tell which animation completed. Instead you have to use the animation's keys to identify your animation.

I have a very elegant solution to providing a custom completion block for each animation, but you need a lot of background on CAAnimation objects in order for it to make sense.
 
Almost three year old thread...

Yup, I realized that AFTER I invested a lot of time in writing a comprehensive reply. Somebody else dusted it off for an update, and I responded to the original question.

Ah well.

The topic is still quite relevant, and should be of fairly broad interest, so it should be time well spent.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.