I have a rotateView setup with a UIRotationGestureRecognizer
. Works as intended, however I would like to only rotate in notches/increments of 90 degrees. The default behavior allows you to be very granular and precise with the rotation. I want it to be the opposite, of only 4 possible positions.
The code I have below is as close I could get, however the problem I am facing is the rotation only happens once, and only to one direction (rotates to the right, even if I 2-fingers rotate to the left).
My code
func rotatedView(recognizer:UIRotationGestureRecognizer){
let pi = CGFloat(M_PI)
rotateView.transform = CGAffineTransformMakeRotation(pi/2)
recognizer.rotation = 0
if recognizer.state == UIGestureRecognizerState.Changed {
print("rotation began")
}
else {
print("rotation ended")
}
}
How can I modify the above code to allow for 90 degree rotation increments in either direction based on gesture?
I achieved this by implementing it as follows:
Note that I added the
UIRotationGestureRecognizer
on the desired view from the Interface Builder, that's why the function is an@IBAction
.Output: