Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode rotate uibutton clockwise

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?

like image 654
CristiC Avatar asked Feb 19 '11 10:02

CristiC


2 Answers

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.

Value: M_PI-0.01
Direction: Clockwise
Reasoning: This is this translates to a rotation of ~179.4, 
which the shortest rotation is thus a positive 179.4 degrees.

And going back to the value given by kishorebjv
Value: -3.141593
Direction: Clockwise
Reasoning: The value is slightly past -180 degrees, recalling PI is 3.1415926
....so the shortest rotation is a positive 179 degrees
like image 148
Lecrte Avatar answered Nov 18 '22 14:11

Lecrte


I'm surprised..I don't know why its happening like that.

Instead of -M_PI give -3.141593.

It 'll rotate clockwise..

as of now its a quick fix.but probably not a exact answer for your question

like image 32
kishorebjv Avatar answered Nov 18 '22 14:11

kishorebjv