Rotate UIButton 360 degrees

无人久伴 提交于 2020-07-17 09:37:18

问题


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

UIView.animateWithDuration(3.0, animations: {
  self.vineTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
  self.instagramTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
})

However, it doesn't rotate 360 degrees because the UIButton is already at that location.

How can I rotate my UIButton 360 degrees?


回答1:


You can use a trick: start rotating with 180 degrees first and then rotate with 360 degrees. Use 2 animations with delay. Try this.

Swift 2

UIView.animateWithDuration(0.5) { () -> Void in
  button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
}

UIView.animateWithDuration(0.5, delay: 0.45, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in      
  button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2))
}, completion: nil)

Swift 3, 4, 5

UIView.animate(withDuration: 0.5) { () -> Void in
  self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
}

UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
  self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 2.0)
}, completion: nil)

Hope this helps




回答2:


As discussed here, you can also use a CAAnimation. This code applies one full 360 rotation:

Swift 3

let fullRotation = CABasicAnimation(keyPath: "transform.rotation")
fullRotation.delegate = self
fullRotation.fromValue = NSNumber(floatLiteral: 0)
fullRotation.toValue = NSNumber(floatLiteral: Double(CGFloat.pi * 2))
fullRotation.duration = 0.5
fullRotation.repeatCount = 1
button.layer.add(fullRotation, forKey: "360")

You will need to import QuartzCore:

import QuartzCore

And your ViewController needs to conform to CAAnimationDelegate:

class ViewController: UIViewController, CAAnimationDelegate {

}



回答3:


in Swift 3 :

UIView.animate(withDuration:0.5, animations: { () -> Void in
  button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
})

UIView.animate(withDuration: 0.5, delay: 0.45, options: .curveEaseIn, animations: { () -> Void in
  button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 2))
}, completion: nil)



回答4:


Swift 4: Animation nested closure is better than animation delay block.

UIView.animate(withDuration: 0.5, animations: { 
  button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) 
 }) { (isAnimationComplete) in

       // Nested Block
UIView.animate(withDuration: 0.5) { 
   button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2)))
       }    
}



回答5:


Swift 4 version that allows you to rotate the same view infinite times:

UIView.animate(withDuration: 0.5) {
  self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
}

if you want to rotate 360°:

UIView.animate(withDuration: 0.5) {
  self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
  self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi)
}



回答6:


You can use a little extension based on CABasicAnimation (Swift 4.x):

extension UIButton {
    func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat(.pi * 2.0)
        rotateAnimation.duration = duration

        if let delegate: AnyObject = completionDelegate {
            rotateAnimation.delegate = delegate as? CAAnimationDelegate
        }
        self.layer.add(rotateAnimation, forKey: nil)
    }
}

Usage:

For example we can start to make a simple button:

let button = UIButton()
button.frame = CGRect(x: self.view.frame.size.width/2, y: 150, width: 50, height: 50)
button.backgroundColor = UIColor.red
button.setTitle("Name your Button ", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)

Then we can build its selector:

@objc func buttonAction(sender: UIButton!) {
        print("Button tapped")
        sender.rotate360Degrees()
}


来源:https://stackoverflow.com/questions/34102331/rotate-uibutton-360-degrees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!