I want to rotate an UIButton at 180 degrees clockwise. But it always rotate counterclockwise.
This is how I tried:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
myButton.transform = CGAffineTransformRotate( myButton.transform, M_PI);
[UIView commitAnimations];
also this:
myButton.transform = CGAffineTransformRotate( myButton.transform, - M_PI);
What am I doing wrong?
I’ve had a similar experience, and my best guess is the following:
The rotation transform translates to a net result, meaning an absolute rotation. Since rotating -PI and +PI results in the same net effect (both 180 degrees), the animation ends up always choosing the default direction; which seems to be counterclockwise on iOS.
By setting it to a value slightly more negative than -M_PI, as @kishorebjv mentioned, the shortest rotation path is through the positive direction (switching the animation to clockwise). You can see this effect by using M_PI+0.01 or M_PI-0.01. Both are positive numbers, but they result in different directions.
More verbose explanation:
Value: M_PI+0.01
Direction: Counterclockwise
Reasoning: This is this translates to a rotation of ~180.6,
which the shortest rotation is thus a negative 179.4 degrees.