私はこの回転機構を直角にスナップしようとしています。ユーザーが近づくと(85〜95度)、彼は85度または95度から離れるまで自動的に90度にスナップします。直角にスナップするUIRotationGestureRecognizer
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)
}
}
- 度が実際に0 ... -180の間に出てくる... 180 ...- 180など – solenoid
また、維持あなたが同じ方向に何度も回っていくと、それは360、720、1080(それがラジアンであるものであれ)になります。あなたが他の方向に進むと、その数は少なくなります。 – solenoid