iOS uiimageView旋转方向在顺时针方向上以可变程度的速度旋转方向



我正在通过此代码旋转图像

UIView.animate(withDuration: 2) {
        self.blueNeedleImageView.transform = CGAffineTransform(rotationAngle: CGFloat(myDegreeValue * Double.pi / 180))
    }

问题是它在最短路径上旋转。我希望总是顺时针旋转和旋转度是可变的。这和这没有提供解决方案。

如果角度大于180°,则将其分为两个动画。

您可以借助于下面的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()
}

最新更新