Alan Moore
Alan Moore

Reputation: 6575

iOS5 CGAffineTransformMakeRotation doesn't work the second time I do it?

I have simple rotation transformation, combined with alpha, which works perfectly on the first call, but the rotation doesn't happen the second time (the user initiates this animation by tapping the screen).

Here is my basic animation function:

- (void) animateMe:(UIImageView *)myImage delay:(NSTimeInterval)dly
{
    [UIView animateWithDuration:1.0 
                          delay:dly
                        options:UIViewAnimationOptionAutoreverse
                     animations:^(void){
                         myImage.alpha = 1.0;
                         myImage.transform = CGAffineTransformMakeRotation(180.0);
                     }
                     completion:^(BOOL finished) {
                         myImage.alpha = 0.0;
                  }];
}

Upvotes: 2

Views: 3087

Answers (1)

sch
sch

Reputation: 27506

The problem is that the second time you want to rotate the view, it is already rotated by 180 degrees and the line:

myImage.transform = CGAffineTransformMakeRotation(180.0);

is equivalent to:

myImage.transform = myImage.transform;

So you should do something like:

myImage.transform = CGAffineTransformRotate(myImage.transform, 180.0);

Note that the documentation says that the rotation angle should be in radians an not degrees. So you probably should use M_PI instead of 180.0.

Also, note that the documentation says that UIViewAnimationOptionAutoreverse must be combined with UIViewAnimationOptionRepeat.

UIViewAnimationOptionAutoreverse

Run the animation backwards and forwards. Must be combined with the UIViewAnimationOptionRepeat option.

Upvotes: 7

Related Questions