I am animating a button using core animation now on a certain condition I want to stop that animation how do I stop the animation?
here is the method to animate the button
-(void)animateButton:(UIButton *)btnName
{
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = FLT_MAX;
[btnName.layer addAnimation:pulseAnimation forKey:nil];
}
From Core Animation Programming Guide
Starting and Stopping Explicit Animations
You start an explicit animation by sending a addAnimation:forKey: message to the target layer, passing the animation and an identifier as parameters. Once added to the target layer the explicit animation will run until the animation completes, or it is removed from the layer. The identifier used to add an animation to a layer is also used to stop it by invoking removeAnimationForKey:. You can stop all animations for a layer by sending the layer a removeAllAnimations message.
As nall points out, you just need to assign a key to your animation (string, etc.), then use -removeAnimationForKey: on your layer to remove that particular animation.
However, if you do this, the layer should revert to its pre-animation state. To have the layer stop with the animated property retaining its current value, you'll want to do what I describe in this answer: get the presentationLayer for the animating layer, read the current value of the animated property, set that value to the animating layer, and only then remove the animation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With