Rotate UIButton 360 degrees

后端 未结 8 1086
青春惊慌失措
青春惊慌失措 2021-02-07 01:52

I\'ve been trying to run an animation that rotates my UIButton 360 degrees using this code:

UIView.animateWithDuration(3.0, animations: {
  self.vin         


        
8条回答
  •  眼角桃花
    2021-02-07 02:24

    You can also try to implement an extension which will simplify things if you need to do it multiple time

    extension UIView {
        
        func rotate360Degrees(duration: CFTimeInterval = 1, repeatCount: Float = .infinity) {
            let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
            rotateAnimation.fromValue = 0.0
            rotateAnimation.toValue = CGFloat(Double.pi * 2)
            rotateAnimation.isRemovedOnCompletion = false
            rotateAnimation.duration = duration
            rotateAnimation.repeatCount = repeatCount
            layer.add(rotateAnimation, forKey: nil)
        }
        
        // Call this if using infinity animation
        func stopRotation () {
            layer.removeAllAnimations()
        }
    }
    

提交回复
热议问题