【发布时间】:2018-01-19 03:02:22
【问题描述】:
我正在尝试使用这种旋转机制来捕捉直角,例如当用户接近(85 到 95 度)时,它会自动捕捉到 90 度,直到他离开 85 或 95 度。
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 等之间
-
另外,请记住 sender.rotation 是累加的,这意味着如果您在同一个方向上绕了很多次,它将是 360、720、1080(无论是多少弧度)。当你走另一条路时,这个数字会降低。
标签: ios swift math uiview rotation