views:

3058

answers:

3

I'm trying to do a multistage animation, so that a UIImageView (1) fades in, (2) moves, (3) slide off the screen.

Only stage 1 seems to work. What am I doing wrong? Here's the code:

// FIRST PART - FADE IN
-(void)firstAnim
{
    // 'sprite' is a UIImageView
    [sprite setAlpha:0.1f];
    [UIView beginAnimations:@"anim1" context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationDidStopSelector:@selector(secondAnim)];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [sprite setAlpha:1.0f];
    [UIView commitAnimations];
}


// SECOND PART - MOVE
-(void)secondAnim
{
    [UIView beginAnimations:@"anim2" context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDidStopSelector:@selector(thirdAnim)];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    sprite.frame = CGRectMake(170, 184, 20, 20);
    [UIView commitAnimations];
}

// THIRD PART - SLIDE OFF SCREEN
-(void)thirdAnim
{   
    [UIView beginAnimations:@"anim3" context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    sprite.frame = CGRectMake(170, 420, 20, 20);
    [UIView commitAnimations];
}
+3  A: 

You need to add a call to set yourself as the animation delegate:

[UIView setAnimationDelegate:self];

It would be a good idea to unset yourself as the delegate (set to nil) in the last animation block.

Kendall Helmstetter Gelner
thanks. i also had to the setAnimationDidStopSelector line to : [UIView setAnimationDidStopSelector:@selector(secondAnim:finished:context:)];
cannyboy
A: 

Hi,

I know this question is 2 months old now but, does anyone know how to make animation like this to respond to touches? I can code for animation and for touches but, having trouble combining them.

All i want is to have a Bee move from off screen to bottom left with animation (automated). Then once there, have the Bee move, responding to touches whilst being animated, i.e flapping its wings to point of touch.

Thank You!

Alex

Alex
The answer area is not the best place to answer questions.
bentford
yeah just start a new question.
nickthedude
+1  A: 

The complete solution to your question is:

1) set the animation delegate

2) use the correct selector and method signature

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationDelegate:self];  //set delegate!
[UIView setAnimationDidStopSelector:
    @selector(secondAnim:finished:context:)];


-(void)secondAnim:(NSString *)animationID 
         finished:(NSNumber *)finished 
          context:(void *)context {

    //animation #2
}
bentford