views:

237

answers:

1

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!

+4  A: 

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.

Christopher Fairbairn
Thanks, did the job perfectly! I've noticed that if I animate the same view again but midway through a current rotation animation, it does not pick up where it is and start moving, it jumps to the end point of the current animation and then begins the new animation. Any idea why?
Michael Waterfall
Did you figure this out? Short answer, wait for current anim to finish before starting your next. See http://stackoverflow.com/questions/2728516/iphone-detect-the-end-of-the-animation/2767229#2767229
brindy