RanLearns
RanLearns

Reputation: 4176

how to execute multiple animations in a row on the same UIImageView

I'd like to do two animations, one after the other:

[UIView beginAnimations:@"growImage" context:nil];
[UIView setAnimationDuration:0.2f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];

[UIView beginAnimations:@"shrinkImage" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
image6.transform = CGAffineTransformMakeScale(0.1, 0.1);
[UIView commitAnimations];

I know that I can do the second animation in

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

or by setting a

[UIView setAnimationDidStopSelector:@selector(shrinkImage)];

But is there a way that I can pass in the value (in this case a UIIMageView named image6) so that the second animation occurs on the same object as the first one?

THANKS!

Upvotes: 1

Views: 5212

Answers (2)

highlycaffeinated
highlycaffeinated

Reputation: 19867

If you want to use the selector-based approach, the selector that you specify in the setAnimationDidStopSelector gets passed a context parameter that you specify in your beginAnimations:context: call.

So, if you change your beginAnimations call to:

[UIView beginAnimations:@"growImage" context:image6];

then image6 will get passed as the context parameter to your selector.

An easier way would be to use blocks to accomplish the same thing:

[UIView animateWithDuration:0.2 animations:^() {
    image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
}
completion:^(BOOL finished) {
    [UIView animateWithDuration:0.2 animations:^() {
        image6.transform = CGAffineTransformMakeScale(0.1, 0.1);
    }];
}];

Upvotes: 2

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

This is an example from my code. But you get the idea. You need to use CGAffineTransformConcat to stitch multiple animation together. You can do this-

   [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    angle             = 0;
    scaleFactor       = 1;
    startPoint.x      = 60.0;
    startPoint.y      = 60.0;
    CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
    CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI/180);
    boxView.center    = startPoint;
    boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);
    [UIView commitAnimations];

iOS4 has Single block animation you could use that instead of Two Block animation. Apple documentation states that Single Block animation is more smooth, plus its easy to stitch multiple animations one after the other...

UPDATE: After thinking more carefully, you could do this -

[UIView animateWithDuration:0.3 
                      delay:0 
                    options:UIViewAnimationCurveEaseOut 
                 animations:^{ /* first level of animation */
                 } 
                 completion:^(BOOL finished){
                 }
];

As you can see, after the animation completes, you can start another animation or you can clean up. This block based animation, is used to chain animations together...

Upvotes: 1

Related Questions