Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate a UIImage or UIView to a particular angle, NOT an amount of angle

Objective-C for iOS.

How do I rotate an image or view TO a particular angle. I have tried the two methods below, but both rotate the AMOUNT of angle, not TO the angle. Basically I would like to call a method to rotate to 90' and then say call again of 180', and the image/view to finish at 180', NOT an addition of them both (270') which is what happens if I use either of the methods below.

#define degreesToRadians(degrees) (M_PI * degrees / 180.0)

myImage.transform = CGAffineTransformMakeRotation(degreesToRadians(90));

myImage.transform = CGAffineTransformRotate(myImage.transform, degreesToRadians(90));

Many thanks!

like image 821
James Beith Avatar asked Jul 14 '10 09:07

James Beith


2 Answers

Reset by

.transform = CGAffineTransformIdentity

before the other transforms...

like image 69
ohho Avatar answered Oct 26 '22 17:10

ohho


ohho's answer can be useful if you haven't to do animation. I found useful this trick:

[UIView animateWithDuration:0.2 animations:^{
            self.compassImgView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, radians);
        }];
like image 38
ricky.tribbia Avatar answered Oct 26 '22 19:10

ricky.tribbia