Girar el button UIB 360 grados – Swift

He estado intentando ejecutar una animation que gira mi UIButton 360 grados, sin embargo cuando hago esto:

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

No gira 360 grados porque el button UIB ya se encuentra en esa location.

¿Cómo puedo girar mi UIButton 360 grados?

Puede usar un truco: comience a girar primero con 180 grados y luego gírelo con 360 grados. Use 2 animaciones con retraso. Prueba esto.

  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 4

  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) 

Espero que esto ayude

en 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) 

Como se menciona aquí , también puede usar una CAAnimation. Este código aplica una rotation completa de 360:

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") 

Deberá importar QuartzCore:

 import QuartzCore 

Y su ViewController debe cumplir con CAAnimationDelegate:

 class ViewController: UIViewController, CAAnimationDelegate { } 

Swift 3.1 : el cierre nested de animation es mejor que el bloque de retardo de animation.

  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))) } } 

Puede usar la extensión UIView ( Swift 3.x ):

 extension UIView { 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) } } 

Uso :

 self.vineTimeCapButton.rotate360Degrees() 

o:

 self. vineTimeCapButton.rotate360Degrees(duration:2)