Rotate a view for 360 degrees indefinitely in Swift?

后端 未结 10 1216
闹比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:55

    Swift 3.0

    let imgViewRing = UIImageView(image: UIImage(named: "apple"))
    imgViewRing.frame = CGRect(x: 0, y: 0, width: UIImage(named: "apple")!.size.width, height: UIImage(named: "apple")!.size.height)
    imgViewRing.center = CGPoint(x: self.view.frame.size.width/2.0, y: self.view.frame.size.height/2.0)
    rotateAnimation(imageView: imgViewRing)
    self.view.addSubview(imgViewRing)
    

    This is the animation logic

    func rotateAnimation(imageView:UIImageView,duration: CFTimeInterval = 2.0) {
            let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
            rotateAnimation.fromValue = 0.0
            rotateAnimation.toValue = CGFloat(.pi * 2.0)
            rotateAnimation.duration = duration
            rotateAnimation.repeatCount = .greatestFiniteMagnitude
    
            imageView.layer.add(rotateAnimation, forKey: nil)
        }
    

    You can check output in this link

提交回复
热议问题