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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With