2

I'm trying to figure out how I can make this last forever. Right now it lasts 1 sec.

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

    if let delegate: AnyObject = completionDelegate {
        rotateAnimation.delegate = delegate
    }
    self.layer.addAnimation(rotateAnimation, forKey: nil)
}

}

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
Miles H.
  • 285
  • 2
  • 13
  • How are you calling this? `self.rotate360Degrees(duration: 2, completionDelegate: nil)` And it runs for 2 seconds exactly. – gran_profaci Apr 09 '15 at 00:01
  • 1
    Try converting the answer here to Swift: http://stackoverflow.com/questions/14730460/how-to-make-uiview-animation-sequence-repeat-and-autoreverse – robinkunde Apr 09 '15 at 01:46

1 Answers1

2

You can add in :

rotateAnimation.repeatCount = Float(CGFloat.max)

Or to reverse and repeat :

rotateAnimation.autoreverse = true // Though this is probably not what you need.

This gave me a forever rotating box.

    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
    rotateAnimation.fromValue = 0.0
    rotateAnimation.toValue = CGFloat(M_PI * 2.0)
    rotateAnimation.duration = duration
    rotateAnimation.autoreverses = true
    rotateAnimation.repeatCount = Float(CGFloat.max)
gran_profaci
  • 8,087
  • 15
  • 66
  • 99