views:

194

answers:

2

Hi I have some a UIView with several user created subviews. The user can move and rotate the subviews. I'm using the transform attribute of UIView to accomplish this. I want to save the "document" in order to recreate it later.

I'm currently writing out the transform attribute out to a file along with some other data. But the transform attribute is 6 number set, instead I would like to somehow get the degrees of rotation that the user made and save that.

Is there any way to take a transform and get back out the total rotation that created the transform? Obviously as the user was moving the object around the screen, many little transforms were done.

A: 

Yeah, read up on The transformation matrix for an affine transformation. So you would need to figure out the quadrant of the transform (which 90-degree section of the circle it's in), and take the reciprocal of the sin() which is the cosecant(), and the reciprocal of the cos() which is the secant(), of a and c of the matrix. I haven't done it myself but it seems pretty easy.

lucius
It's been awhile since I've done any geometry. I'll have to give it a try when I have time.
agilpwc
+1  A: 

There is an easier way than even the math that lucius points to. You can use a helper keypath that Core Animation provides to extract the current rotation of a UIView's backing layer:

CGFloat rotationAngle =  [[view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];

This will give you the current rotation of the view, in radians. Note that this reports positive values for the first clockwise semicircle of rotation, and negative values for the second, so you will need to account for that in your calculations.

Brad Larson
This is very easy to implement. Thanks!
agilpwc