Swift--使圖片360° 周期旋轉

來源:互聯網
上載者:User

標籤:transform   import   class   remove   port   content   組合   lan   prot   

UIImageView+Extension.swift
import UIKitextension UIImageView {    // 360度旋轉圖片    func rotate360Degree() {        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z") // 讓其在z軸旋轉        rotationAnimation.toValue = NSNumber(double: M_PI * 2.0) // 旋轉角度        rotationAnimation.duration = 0.6 // 旋轉周期        rotationAnimation.cumulative = true // 旋轉累加角度        rotationAnimation.repeatCount = 100000 // 旋轉次數        layer.addAnimation(rotationAnimation, forKey: "rotationAnimation")    }    // 停止旋轉    func stopRotate() {        layer.removeAllAnimations()    }}

上面的代碼使用了CABasicAnimation做動畫,組合了UIImageView擴充,使UIImageView中的單幅圖片圍繞Z軸做周期旋轉。

直接把上述代碼加到UIImageView擴充中,並設定旋轉周期和旋轉次數(可設定repeatCount為最大值使其一直旋轉),然後調用即可。

Swift--使圖片360° 周期旋轉

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.