views:

8280

answers:

5

I know how to move a layer based on touch. But I would also like to be able to rotate the image.

Is there any sample code that shows how to do this? Or can anyone give me some advice?

Thanks!

+1  A: 

You would use the view's transform property. There's some example code for rotating the view in the iPhone OS Programming Guide, under Launching in Landscape Mode

Zydeco
A: 

You should look at Apple's MoveMe example for how to move around a layer based on touch. It also applies some scaling transforms as you do it, so that should serve as a reasonable example of to apply rotation transforms.

Louis Gerbarg
+5  A: 

The simplest way to do this is using the layer's transform property:

float   angle = M_PI;  //rotate 180°, or 1 π radians
layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);

The first argument to the CATransform3DMakeRotation function is the amount to rotate, in radians. The next three describe the vector around which to rotate. This is describing a vector in the z-axis, so effectively perpendicular to the screen. This will rotate the layer so it's upside down.

Ben Gottlieb
+1  A: 

I ended up doing it like this:

CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
[[self viewWithTag:999] setTransform:transform];

Note that the angle is in radians.

rksprst
In your solution, you are modifying the transform of the view, not the layer. Just an fyi.
Ben Gottlieb
A: 

I ended up doing it like this:

CGAffineTransform transform = CGAffineTransformMakeRotation(angle); [[self viewWithTag:999] setTransform:transform];

Note that the angle is in radians.

--------->>>> Hi is it working i did same like this, i am trying to rotate snooker cue with touches , getting the angle but I am doing like CGAffineTransform transform = CGAffineTransformMakeRotation(angle); myImage.transform = transform;

it is not working, is this way is wrong or i should try [myImage setTransform: transform];

please update me, I am getting nothing no effects at all. what should i do ?