views:

1186

answers:

2

I am writing an iPhone programer, and I want to make a button with is rotate 180 degree, I try to use the multi-touch track pad to rotate a UIbutton, but it don't success, how can I do it? or I need to do it by code?

A: 

You can't do it from Interface Builder. You have to rotate it from your code, using the transform property of your UIButton, which is a CGAffineTransform struct. You can use the CGAffineTransformMakeRotation() to set it.

myButton.transform = CGAffineTransformMakeRotation( ( 180 * M_PI ) / 180 );

The first 180 in the code is the angle in degrees. The operation converts it to radians.

Macmade
A: 

Well, here we go:

CABasicAnimation *halfTurn;
halfTurn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
halfTurn.fromValue = [NSNumber numberWithFloat:0];
halfTurn.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
halfTurn.duration = 0.5;
halfTurn.repeatCount = 1;
[myButton addAnimation:halfTurn forKey:@"180"];

Hope that helps... Im typing from my PC though, not my Mac - So I hope that's right!

Neurofluxation
Wow... Keep it simple!Why using an animation to rotate a single element, since you can do it using a single property of the object? IMHO, it's useless, it made to code uselessly complex, and it not good at all for performance...
Macmade
This code functions, and is a *nice* standard way to rotate an object - just ENSURE that you RELEASE it all afterwards.
Neurofluxation
It did not say your code does not work. I'm just saying this can be done in another way, which is easier and more performant.
Macmade
I think there was just a fundamental ambiguity in the original question. Does "rotate" mean "transform to a rotated position", or does it make "make it rotate" as in an animation. I took it the first way, as Macmade evidentally did. It looks like you took it the second way - which is equally valid, but leads to a different answer.
Phil Nash