簡體   English   中英

使用CAKeyframeAnimation反轉UIImageView的旋轉

[英]Reverse the rotation of UIImageView using CAKeyframeAnimation

我正在使用以下代碼無限旋轉圖像,旋轉順時針,但我還需要它反轉旋轉到逆時針每1-2轉,然后回到順時針,如何讓它工作?

    /// Image Rotation - CAKeyframeAnimation
func rotate(imageView: UIImageView, rotationSpeed: Double) {

    let animation = CAKeyframeAnimation(keyPath: "transform.rotation.z")

    animation.duration = rotationSpeed
    animation.fillMode = CAMediaTimingFillMode.forwards
    animation.repeatCount = .infinity
    animation.values = [0, Double.pi/2, Double.pi, Double.pi*3/2, Double.pi*2]

    /// Percentage of each key frame
    let moments = [NSNumber(value: 0.0), NSNumber(value: 0.1),
                   NSNumber(value: 0.3), NSNumber(value: 0.8), NSNumber(value: 1.0)]
    animation.keyTimes = moments

    imageView.layer.add(animation, forKey: "rotate")
}

設置animation.autoreverses = true ,將反轉動畫。

對於逆時針旋轉,您可以這樣做:

self.imageView?.transform = CGAffineTransform(rotationAngle: -2*CGFloat.pi)

暫無
暫無

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

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