PDA

View Full Version : About transform and CGAffineTransformMakeRotation

mikezang
Jan 14, 2011, 05:52 PM
I am using code as below to rotate a UIView for 90 degrees, when I want to rotate 90 degrees again from last position and use the same code, nothing happened.

view.transform = CGAffineTransformMakeRotation(M_PI / 2);

But when I change to below, it rotate 90 degree from last position, I want to know why, I checked Programming Guide and Class Reference about UIView, CGContext and CGAffineTransform, but still not clear, hope you can answer my question.

view.transform = CGAffineTransformMakeRotation(M_PI);

robbieduncan
Jan 14, 2011, 05:59 PM
Setting the transform is not cumulative.

view.transform = CGAffineTransformMakeRotation(M_PI);

sets the transform M_PI

view.transform = CGAffineTransformMakeRotation(M_PI);
view.transform = CGAffineTransformMakeRotation(M_PI);

sets the transform to M_PI and then sets it to M_PI again. Which is clearly redundant.

It's just a property. It's never going to set it to 2*M_PI. Why would you expect it to work any differently?

mikezang
Jan 14, 2011, 06:15 PM
Sorry I made mistake, the first code os M_PI / 2, then is M_PI.

I use that code to animate my view, first view rotate 90 degrees in animation, so I think if I animated two times, view should rotate 180 degrees from original position, and rotate 90 degrees from position after first rotation!

But View only rotate 90 degrees even if I run same code more than two times, then I change code to M_PI after first rotation, this time view rotate more 90 degrees, now I got what I need.

The problem is why I have to set 180 degrees in second time, can you answer me?

robbieduncan
Jan 14, 2011, 06:17 PM
I repeat: you are setting a property. When you set the rotation to 90º that's what happens. If you set it to 90º again it doesn't suddenly become 180º. You are not calling a command that rotates by an amount. You are setting it to a specific amount.

It's just as if you were setting an integer. If you set a property to 10 and then set it to 10 again you'd be really surprised and upset if the property value was 20.

mikezang
Jan 14, 2011, 06:48 PM
Well, I understood. thank you very much.

So can you tell me how I can let view rotate 90 in real, so that I can rotate 90 again let it in 180?

Maybe use CGContextRotateCTM?

I think CGAffineTransformRotate might be what I need, I will try after a few hours, as I have to sleep now, I am sit here since yesterday 1800.