xcode 如何停止核心动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1566542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:54:10  来源:igfitidea点击:

how to stop core animation?

objective-cxcodeiphone-sdk-3.0core-animation

提问by Rahul Vyas

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];
}

回答by nall

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.

您可以通过向目标层发送 addAnimation:forKey: 消息,将动画和标识符作为参数传递来启动显式动画。一旦添加到目标层,显式动画将运行直到动画完成,或者从层中删除。用于向图层添加动画的标识符也用于通过调用 removeAnimationForKey: 来停止动画。您可以通过向图层发送 removeAllAnimations 消息来停止图层的所有动画。

回答by Brad Larson

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.

正如纳尔指出的那样,您只需要为您的动画(字符串等)分配一个键,然后在您的图层上使用 -removeAnimationForKey: 来删除该特定动画。

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.

但是,如果您这样做,图层应该恢复到其动画前状态。要让动画属性保持其当前值停止图层,您需要执行我在此答案中描述的操作:获取动画图层的presentationLayer,读取动画属性的当前值,将该值设置为动画层,然后才删除动画。