Chain of animation blocks

Discussion in 'iOS Programming' started by JGoose, Apr 17, 2010.

  1. JGoose macrumors member

    Joined:
    Feb 12, 2010
    #1
    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.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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?
     
  3. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #3
    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.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     

    Attached Files:

  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    You just set the animationDidStopSelector to a method that starts a second animation after the first one is done.
     
  6. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #6
    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];
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Because that will fade it out at the same time as moving it. The OP wants a two-phase animation: movement then fading.
     
  8. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #8
    Your example is just what i needed, thank you. I got it working now.
     
  9. mannly01, Oct 19, 2012
    Last edited: Oct 19, 2012

    mannly01 macrumors newbie

    Joined:
    Oct 19, 2012
    Location:
    Las Vegas, NV
    #9
    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.
     

    Attached Files:

  10. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #10
    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.
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  12. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #12
    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.
     

Share This Page