Search code examples
iosobjective-cuiviewanimationcgaffinetransform

Can't rotate Image when button is clicked more than once


I am trying to Rotate an ImageView every time the button is clicked. It only rotates for the first time the button is clicked, otherwise it is nonchalant

- (IBAction)btnRotate:(id)sender
{
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^
    {
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
        self.image.transform = transform;
    } completion:NULL];
}

Also I want to rotate image from the state it was last rotated, I mean, if I rotate image to 90 degrees, when the button is clicked next time, the image should rotate from 90 degrees to 180 degrees.


Solution

  • Do this :

    CGAffineTransform transform = self.image.transform;
    CGAffineTransform transform_new = CGAffineTransformRotate(transform, M_PI);
    self.image.transform = transform_new;
    

    instead of :

    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
    self.image.transform = transform;
    

    Every time you press the button, you need to take into consideration the previous rotation.