Reputation: 2408
I'm using a code snippit from the Stack Overflow post: How to make "shaking" animation
I pasted this code into my project and it works perfectly:
func shakeView(){
var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 2
shake.autoreverses = true
var from_point:CGPoint = CGPointMake(LoginField.center.x - 5, LoginField.center.y)
var from_value:NSValue = NSValue(CGPoint: from_point)
var to_point:CGPoint = CGPointMake(LoginField.center.x + 5, LoginField.center.y)
var to_value:NSValue = NSValue(CGPoint: to_point)
shake.fromValue = from_value
shake.toValue = to_value
LoginField.layer.addAnimation(shake, forKey: "position")
}
I however want this animation to loop until another function is called. How do I make it loop indefinite and how can I make it stop when another function is called?
Upvotes: 0
Views: 2218
Reputation: 57184
First, set the repeatCount
to HUGE_VALF
/ greatestFiniteMagnitude
:
Setting this property to HUGE_VALF will cause the animation to repeat forever.
Second, call LoginField.layer.removeAllAnimations()
or LoginField.layer.removeAnimationForKey("position")
when you want to stop the animation.
Edit: Apparently Swift does not know HUGE_VALF
, setting the repeatCount
to Float.infinity
should work.
Upvotes: 3