I am trying to have this rotation mechanism to snap on right angles, e.g. when the user gets close to (85 to 95 degrees) it automatically snaps to 90 degrees until he moves away from 85 or 95 degrees.
var lastRotation = CGFloat()
func rotateAction(sender:UIRotationGestureRecognizer){
let currentTransform = sender.view?.transform
let rotation = 0.0 - (lastRotation - sender.rotation)
let newTransform = currentTransform!.rotated(by: rotation)
let radians = atan2f(Float(sender.view!.transform.b), Float(sender.view!.transform.a))
let degrees = radians * (180 / .pi)
sender.view?.transform = newTransform
lastRotation = sender.rotation
if sender.state == .ended {
lastRotation = 0.0;
}
// The if statement works correctly when reaching the angles
if degrees > -95 && degrees < -85 {
}
else if degrees > -185 && degrees < -175 {
}
else if degrees > -275 && degrees < -265 {
}
else if degrees > -5 && degrees < 5 {
// So I tried this but it does not seem right, it always pushed it away from angle 0
lastRotation = CGFloat(0.0 - radians)
}
}