簡體   English   中英

ios UIImageView旋轉方向以順時針方向以可變程度快速旋轉

[英]ios UIImageView rotation direction in swift at variable degree in clockwise direction

我正在通過此代碼旋轉圖像

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()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM