Swift - 旋转手势和旋转增量为 90 度
Swift - Rotate gesture and rotation increments of 90 degrees
我有一个带有 UIRotationGestureRecognizer
的 rotateView 设置。按预期工作,但我只想旋转 notches/increments 90 度。默认行为允许您非常精细和精确地进行旋转。我希望它是相反的,只有 4 个可能的位置。
我下面的代码是我能得到的最接近的代码,但是我面临的问题是旋转只发生一次,而且只向一个方向旋转(向右旋转,即使我用 2 个手指旋转到左)。
我的代码
func rotatedView(recognizer:UIRotationGestureRecognizer){
let pi = CGFloat(M_PI)
rotateView.transform = CGAffineTransformMakeRotation(pi/2)
recognizer.rotation = 0
if recognizer.state == UIGestureRecognizerState.Changed {
print("rotation began")
}
else {
print("rotation ended")
}
}
如何修改以上代码以允许基于手势在任一方向上进行 90 度旋转增量?
我是通过如下方式实现的:
@IBAction func handleRotation(_ recognizer: UIRotationGestureRecognizer) {
if let recognizerView = recognizer.view {
recognizerView.transform = recognizerView.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
let radians:Double = atan2( Double(recognizerView.transform.b), Double(recognizerView.transform.a))
let degrees = radians * Double((180 / Float.pi))
if recognizer.state == .ended || recognizer.state == .cancelled {
var degreeToAnimate:CGFloat = 0
switch degrees {
case -45...45:
print("the default value 0, no need to any assign...")
case 46...135:
degreeToAnimate = CGFloat(M_PI_2)
case 136...180, -180 ... -136:
degreeToAnimate = CGFloat(M_PI)
case -135 ... -46:
degreeToAnimate = CGFloat(-M_PI_2)
default:
print("!")
}
UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1.0, options: .curveEaseIn, animations: {
recognizerView.transform = CGAffineTransform(rotationAngle: degreeToAnimate)
}, completion: { _ in
recognizer.rotation = 0
})
}
}
}
请注意,我在 Interface Builder 的所需视图上添加了 UIRotationGestureRecognizer
,这就是该函数是 @IBAction
.[=14= 的原因]
输出:
我有一个带有 UIRotationGestureRecognizer
的 rotateView 设置。按预期工作,但我只想旋转 notches/increments 90 度。默认行为允许您非常精细和精确地进行旋转。我希望它是相反的,只有 4 个可能的位置。
我下面的代码是我能得到的最接近的代码,但是我面临的问题是旋转只发生一次,而且只向一个方向旋转(向右旋转,即使我用 2 个手指旋转到左)。
我的代码
func rotatedView(recognizer:UIRotationGestureRecognizer){
let pi = CGFloat(M_PI)
rotateView.transform = CGAffineTransformMakeRotation(pi/2)
recognizer.rotation = 0
if recognizer.state == UIGestureRecognizerState.Changed {
print("rotation began")
}
else {
print("rotation ended")
}
}
如何修改以上代码以允许基于手势在任一方向上进行 90 度旋转增量?
我是通过如下方式实现的:
@IBAction func handleRotation(_ recognizer: UIRotationGestureRecognizer) {
if let recognizerView = recognizer.view {
recognizerView.transform = recognizerView.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
let radians:Double = atan2( Double(recognizerView.transform.b), Double(recognizerView.transform.a))
let degrees = radians * Double((180 / Float.pi))
if recognizer.state == .ended || recognizer.state == .cancelled {
var degreeToAnimate:CGFloat = 0
switch degrees {
case -45...45:
print("the default value 0, no need to any assign...")
case 46...135:
degreeToAnimate = CGFloat(M_PI_2)
case 136...180, -180 ... -136:
degreeToAnimate = CGFloat(M_PI)
case -135 ... -46:
degreeToAnimate = CGFloat(-M_PI_2)
default:
print("!")
}
UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1.0, options: .curveEaseIn, animations: {
recognizerView.transform = CGAffineTransform(rotationAngle: degreeToAnimate)
}, completion: { _ in
recognizer.rotation = 0
})
}
}
}
请注意,我在 Interface Builder 的所需视图上添加了 UIRotationGestureRecognizer
,这就是该函数是 @IBAction
.[=14= 的原因]
输出: