Nadal Alyafaie Nadal Alyafaie - 9 days ago 6
Swift Question

Rotate UIButton 360 degrees - Swift

I have been trying to run an animation that rotates my UIButton 360 degrees, however when i do this:

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


It doesn't rotate 360 degrees because the UIButton is already at that location.

How can i rotate my UIButton 360 degrees?

Answer

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

    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)

Hope this help