簡體   English   中英

快速動畫旋轉 UIImageView

[英]animate rotation UIImageView in swift

我正在嘗試在 Swift 中為UIImageView的 180 度旋轉設置動畫

    UIView.animateWithDuration(1.0, animations: { () -> Void in
        self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
    }) { (succeed) -> Void in

    }

但根本沒有動畫。 我想使用 animateWithDuration 因為我想在某個時候使用CGAffineTransformMakeIdentity讓它回來

使用UIView動畫有效。

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIView.setAnimationCurve(UIViewAnimationCurve.EaseIn)
    let radians = CGFloat(180 * M_PI / 180)
    arrowImageView.transform = CGAffineTransformMakeRotation(radians)
    UIView.commitAnimations()

======= UIView 的擴展========

我從互聯網上找到了這個答案,並對其進行了測試,效果很好。 希望這對任何人myUIView.rotate()幫助,只需將UIView更改為您的需要,例如UIView、UIButton、UIImageView等,並使用myUIView.rotate()函數訪問,這里myUIView替換您的視圖名稱(IBOutlet ->名稱)具有正確的擴展視圖類型。 這是我找到這個答案的鏈接 而且這個方法簡單有效!

=== SWIFT 3 / 4 ===

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(double: M_PI * 2)
        rotation.duration = 1
        rotation.cumulative = true
        rotation.repeatCount = FLT_MAX
        self.layer.addAnimation(rotation, forKey: "rotationAnimation")
    }
}

建議的改進。 感謝您的所有建議。

=== SWIFT 5 ===

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        self.layer.add(rotation, forKey: "rotationAnimation")
    }
}

然后調用該擴展,如下所示:

self.your_UIViewOutletName_myUIView_here.rotate()

這是旋轉圖像視圖的代碼。 迅捷 3.0

UIView.animate(withDuration:2.0, animations: {
            self.dropDownImage.transform = CGAffineTransform(rotationAngle: CGFloat(imageRotation))
        })

更新 swift 4.0 版本:

    let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotation.toValue = Double.pi * 2
    rotation.duration = 0.25 // or however long you want ...
    rotation.isCumulative = true
    rotation.repeatCount = Float.greatestFiniteMagnitude
    yourView.layer.add(rotation, forKey: "rotationAnimation")

首先,如果您想旋轉 180 度,則必須轉換為弧度。 180 度的弧度是pi 360 度將是2 * pi 180 * pi將使您的動畫在一秒鍾內旋轉 90 次左右,並以原始方向結束。

其次,我不確定為什么您的代碼不起作用,但我知道下面的代碼確實有效:

let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = M_PI
rotationAnimation.duration = 1.0

self.arrowImageView.layer.addAnimation(rotationAnimation, forKey: nil)

斯威夫特 5.0

extension UIImageView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 2
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        self.layer.add(rotation, forKey: "rotationAnimation")
    }
}

更新到@Rakshitha Muranga Rodrigo 的回答。

您在CGFloat(180 * M_PI)上遺漏了/ 180 嘗試:

UIView.animate(withDuration: 1.0) {[weak self] in
    self?.arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
}

一定要在主線程中

DispatchQueue.main.async {
   UIView.animateWithDuration(1.0, animations: { () -> Void in
     self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
   }) { (succeed) -> Void in

   }
}

對我來說,最好和最短的解決方案是(Swift 3/4):

self.YourImageView.transform = showing ? CGAffineTransform(rotationAngle: CGFloat.pi) : CGAffineTransform.identity

“顯示”這是一個確定圖像之前是否旋轉的值 - 您可以在此處使用自己的邏輯

90度旋轉

var shouldRotateArrow = false {
    didSet {
        rotateArrow()
    }
}

private func rotateArrow() {
    let angle: CGFloat = shouldRotateArrow ? .pi / 2 : 0
    UIView.animate(withDuration: Constants.arrowRotationDuration) {
        self.arrowImageView.transform = CGAffineTransform(rotationAngle: angle)
    }
}
To start spinning ImageView
-----------------

func startSpinning()  {
            UIView.animate(withDuration: 0.5, delay: 0, options: .repeat, animations: { () -> Void in
                self.gearImageView!.transform = self.gearImageView!.transform.rotated(by: .pi / 2)
                })
        }



To stop spin ImageView
-------------

func stopSpinning()  {
        self.gearImageView.layer.removeAllAnimations()
    }

嘗試調用 arrowImageView.layoutSubviews(),見下文:

    UIView.animateWithDuration(1.0, animations: { () -> Void in
        self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
        self.arrowImageView.layoutSubviews()
    }) { (succeed) -> Void in

    }

暫無
暫無

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

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