Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop core animation?

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];
}
like image 918
Rahul Vyas Avatar asked Oct 14 '09 14:10

Rahul Vyas


2 Answers

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.

like image 54
nall Avatar answered Oct 19 '22 07:10

nall


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.

like image 38
Brad Larson Avatar answered Oct 19 '22 07:10

Brad Larson