Search code examples
iphonecore-animation

Simple Core Animation View Rotation


I'm going to look into Core Animation in more detail soon but at the moment I'm just looking to rotate a view by a specified angle. I was wondering if anyone could point me to some code or provide a simple example as a quick search online hasn't pulled up any thing useful!

Basically, I need to set an anchor point and then rotate my view by a certain angle. Then from there I may set another angle and it will animate smoothly to the new angle.

Thanks for your help!


Solution

  • Try something like the following (untested since I'm away from my desk):

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.0];
    
    // Modify any animatable properties
    myImage.transform = CGAffineTransformMakeRotation(90 * M_PI / 180.0);
    
    [UIView commitAnimations];
    

    This will rotate your UIImageView (myImage) 90 degrees over 5 seconds, using the center of the image as the rotation point.

    If you want to rotate around an arbitary point, you could look into altering the layer's anchorPoint (myImage.layer.anchorPoint). It defaults to 0.5,0.5 which leads the middle of the control. Refer to the Core Animation documentation on how this all works.