Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate UIButton 360 degrees

Tags:

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?

like image 944
Nadal Alyafaie Avatar asked Dec 05 '15 07:12

Nadal Alyafaie


1 Answers

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

like image 143
truongky Avatar answered Sep 18 '22 15:09

truongky