Rotate a view for 360 degrees indefinitely in Swift?

后端 未结 10 1194
闹比i
闹比i 2020-12-05 13:06

I want to rotate an image view for 360 degrees indefinitely.

UIView.animate(withDuration: 2, delay: 0, options: [.repeat], animations: {
    self.view.transf         


        
10条回答
  •  既然无缘
    2020-12-05 13:33

    Use this extension to rotate UIImageView 360 degrees.

    extension UIView {
    func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat(M_PI)
        rotateAnimation.duration = duration
    
        if let delegate: CAAnimationDelegate = completionDelegate as! CAAnimationDelegate? {
            rotateAnimation.delegate = delegate
        }
        self.layer.addAnimation(rotateAnimation, forKey: nil)
    }
    }
    

    Than to rotate UIImageView simply use this method

    self.YOUR_SUBVIEW.rotate360Degrees()
    

提交回复
热议问题