我正在通过此代码旋转图像
UIView.animate(withDuration: 2) {
self.blueNeedleImageView.transform = CGAffineTransform(rotationAngle: CGFloat(myDegreeValue * Double.pi / 180))
}
答案 0 :(得分:2)
如果角度大于180°,只需将其分为两个动画即可。
答案 1 :(得分:1)
您可以在CoreAnimation
的帮助下根据需要旋转图像,如下所示:
let angle = myDegreeValue
if let n = NumberFormatter().number(from: angle) {
let floatAngle = Double(truncating: n)
CATransaction.begin()
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.byValue = NSNumber(value: floatAngle * (Double.pi / 180))
rotationAnimation.duration = 2
rotationAnimation.isRemovedOnCompletion = true
CATransaction.setCompletionBlock {
self.blueNeedleImageView.transform = CGAffineTransform(rotationAngle: CGFloat(floatAngle * (Double.pi / 180)))
}
self.blueNeedleImageView.layer.add(rotationAnimation, forKey: "rotationAnimation")
CATransaction.commit()
}