PDA

View Full Version : Gesture rotate snap to angle




nashyo
Sep 9, 2012, 03:13 AM
I can't find a solution online for snapping an image to an angle depending on the current angle of the image, after rotation has finished.

How do you find the angle of a view?

This is the callback method my rotation gesture uses.
-(void)rotateGestureRecognized:(UIRotationGestureRecognizer*)recognizer
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}

The image rotates smooth, but I want the image to snap to 90, 180, 270 or 0 degrees, depending on which it is nearest to, after the user lifts their fingers off the screen.

I can't even start coding it myself because I can't find the current angle of the image view before or after rotation. My views are arranged in a xib as so:

view - > scroll view - > image view

i have tried using the tan cos sin methods to no avail. using NSLog the debug panel shows values of 0 each time. I have tried to cumulate the rotation into a variable (before recognizer.rotation is set to zero in above method) but the values are not consistent e.g. a 90 rotation will return random numbers like 2.9 or 142.0.

How do you find the angle of a view?



Duncan C
Sep 9, 2012, 08:12 AM
I can't find a solution online for snapping an image to an angle depending on the current angle of the image, after rotation has finished.

How do you find the angle of a view?

This is the callback method my rotation gesture uses.
-(void)rotateGestureRecognized:(UIRotationGestureRecognizer*)recognizer
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}

The image rotates smooth, but I want the image to snap to 90, 180, 270 or 0 degrees, depending on which it is nearest to, after the user lifts their fingers off the screen.

I can't even start coding it myself because I can't find the current angle of the image view before or after rotation. My views are arranged in a xib as so:

view - > scroll view - > image view

i have tried using the tan cos sin methods to no avail. using NSLog the debug panel shows values of 0 each time. I have tried to cumulate the rotation into a variable (before recognizer.rotation is set to zero in above method) but the values are not consistent e.g. a 90 rotation will return random numbers like 2.9 or 142.0.

How do you find the angle of a view?

A couple of things. First, I'm fairly certain that rotation gesture recognizers return a value in radians, not degrees. You want to keep your rotation in radians, and only convert to degrees if you want to display the value for the user.

It's easy to convert between radians and degrees:

degrees = fmodf(radians * 180 / M_PI, 360);

Or to convert the other way:

radians = degrees / 180 * M_PI;


You should keep track of the amount of rotation. Create a rotation iVar, and add the change in rotation from the gesture recognizer to it. Then use that to create a transform for the view:

self.rotation += recognizer.rotation;
recognizer.view.transform = CGAffineTransformMakeRotation(self.rotation);