Rotate UIImage in swift

后端 未结 8 1457
一整个雨季
一整个雨季 2021-02-04 10:35

I want to rotate UIImage in clockwise direction. But my current code do not perform function accurate some time it rotate and some time its skip rotation. I want my UIImage will

8条回答
  •  醉酒成梦
    2021-02-04 10:40

    You can create a function like this to make your image rotate in clockwise forever:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            let kRotationAnimationKey = "com.myapplication.rotationanimationkey"
    
            func rotateView(view: UIView, duration: Double = 1) {
                if view.layer.animationForKey(kRotationAnimationKey) == nil {
                    let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
    
                    rotationAnimation.fromValue = 0.0
                    rotationAnimation.toValue = Float(M_PI * 2.0)
                    rotationAnimation.duration = duration
                    rotationAnimation.repeatCount = Float.infinity
    
                    view.layer.addAnimation(rotationAnimation, forKey: kRotationAnimationKey)
                }
            }
          rotateView(self.imageView)
    }
    

    Result is:

提交回复
热议问题