How to create multi-stage animation of UIImageView?

I'm trying to make a multi-stage animation, so that UIImageView (1) disappears, (2) moves, (3) moves from the screen.

Only stage 1 seems to work. What am I doing wrong? Here is 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
source share
2 answers

You need to add a call to establish yourself as an animation delegate:

[UIView setAnimationDelegate:self];

It would be nice to disable yourself as a delegate (set to nil) in the last block of the animation.

+4
source

Complete solution to your question:

1) set animation delegate

2) use the correct selection and signature of the method

[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
}
+4
source

Source: https://habr.com/ru/post/1710518/


All Articles