views:

509

answers:

1

Hi,

I need to rotate in image multiple times in 90 degrees I am using following code,

CGAffineTransform transform = CGAffineTransformMakeRotation(PI/2);
shape.transform = transform;

here shape being my UIImageview.

The problem is every time i click on rotate button it rotates it from its original position, which is not what I want. I would it to rotate from the new position.

I am new to iphone programming, apologies if I am missing on something very basic and thanks in advance for you help.

Kedar

+2  A: 

If you are trying to animate this rotation, you might want to read my answer to this question, where someone was attempting to animate an incremental rotation of a view. It turns out that incremental animation of a transform using CABasicAnimations is a little trickier than you would expect. You first need to read the current transform of your UIView's layer from its presentationLayer, then feed that into your animation as the fromValue.

However, in your case it appears that you are using a UIView animation block to animate the transform property of your view. The code as you have written it would only keep setting your view's transform to the same 90 degree rotation, it wouldn't incrementally rotate the view by 90 degree steps. To do an incremental rotation, I believe you'd need something like the following:

CGAffineTransform transform = CGAffineTransformRotate(shape.transform, M_PI / 2.0f);
shape.transform = transform;

which will grab the current transform of the view, rotate it by 90 degrees, and then apply the resulting transform to the view. Again, this may run into the same issues as with the CABasicAnimations, in which case you should consult the above-linked answer.

Brad Larson
Thanks brad. you code snippet worked just fine. You are the man!
Kedar
Glad to help. If that did indeed solve your problem, you might want to check the "accepted" mark to the left of the answer.
Brad Larson