Search code examples
iphonecocoa-touchuiimageviewcore-animationrotation

How to animate UIImageView rotation few times in a row


I'm trying to animate an UIImage view, each time by 90 degree clockwise. For some reason, the first rotation animation works, but then it stops (even though the other view resizing animation continue to work). Why is that? (I looked everywhere but couldn't find an answer)

{
        [btn setFrame:CGRectMake(previousX, previousY, BUTTON_WIDTH, BUTTON_HEIGHT)];
        [self.view insertSubview:btn atIndex:0];

        [UIView beginAnimations:@"ButtonAnimation" context:nil];

        [UIView setAnimationDuration:2.5];

        [UIView setAnimationCurve:UIViewAnimationCurveLinear];

        CGRect rect = CGRectMake(previousX,previousY+BUTTON_HEIGHT,BUTTON_WIDTH,BUTTON_HEIGHT);

         btn.frame = rect;

        [UIView setAnimationBeginsFromCurrentState:YES]; 

        ++ButtonCounter;
         if (ButtonCounter < [btnArray count])
            {

                [UIView setAnimationDidStopSelector:@selector(didStopReal)];
            }
            else
            {
                ButtonCounter = 0; 
            }


        // The transform matrix
        CGAffineTransform transform = 
        CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));

        imgArrowView.transform = transform;


        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
}

-(void) didStopReal
{
    [self animteReal:ButtonCounter];
}

Solution

  • Build your transform matrix relative to the current transform:

    CGAffineTransform transform = 
    CGAffineTransformRotate(imgArrowView.transform, DEGREES_TO_RADIANS(90));