我正在尝试使用这种旋转机制来捕捉直角,例如当用户接近(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...0 等)。
另一个问题是 sender.rotation 是累积的,这意味着它会在您旋转时不断增加或减少弧度。
一旦其他数学被修复,我会建议类似于以下内容来检查“快照”。
let rad = fabs(sender.rotation.truncatingRemainder(dividingBy: 2 * CGFloat.pi))
print("rotation", sender.rotation, degrees, rad)
switch rad {
case 1.48353...1.65806 :
print("do things")
case 3.05433...3.22886 :
print("do things")
case 4.62512...4.79966 :
print("do things")
case 0...0.0872665 :
print("this is check one of 2")
case 6.19592...6.28319 :
print("this is check two of 2")
default:
print("do other things")
}
编辑:https://developer.apple.com/documentation/uikit/uirotationgesturerecognizer/1624337-rotation
关于ios - UI 旋转手势识别器捕捉直角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620889/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |