我試圖讓這種旋轉機制捕捉直角,當用戶接近(85到95度)時,它會自動對齊到90度,直到他離開85度或95度。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)
}
}
我會打印出你的數學,你會 - 實際上度出來到-180之間... 0 ... 180 ...- 180等 – solenoid
另外,請記住sender.rotation是加法的,這意味着如果你在同一個方向上一堆次,它將是360,720,1080(無論是以rad爲單位)。隨着你走向另一條路,這個數字會降低。 – solenoid