iOS Snippets / Swift

Extending UIView to easily animate infinite rotation

iOS Snippets / Swift

If you want to have infinite rotation ready for use for every UIView, you can extend its definition


extension UIView {
public func startInfiniteRotation() {
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = Float(M_PI * 2.0)
rotationAnimation.duration = 1
rotationAnimation.repeatCount = Float.infinity

self.layer.addAnimation(rotationAnimation, forKey: "infiniteRotationAnimation")
}

public func stopInfiniteRotation(view: UIView) {
if view.layer.animationForKey("infiniteRotationAnimation") != nil {
view.layer.removeAnimationForKey("infiniteRotationAnimation")
}
}
}

Categories: iOS Snippets, Swift
Read more:

Post a new comment