ios 使用 BOOL / 完成块停止自动反转 / 无限重复 UIView 动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13991465/
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-08-30 21:35:45  来源:igfitidea点击:

Stop an auto-reverse / infinite-repeat UIView animation with a BOOL / completion block

iosobjective-ccore-animationuiviewanimationcompletion-block

提问by Luke

I'm setting up the following UIView animateWithDuration:method, with the intention of setting my animationOnBOOL elsewhere in the program to cancel that infinite looped repeat. I was under the impression that the completionblock would be called each time a cycle of the animation ends, but this doesn't appear to be the case.

我正在设置以下UIView animateWithDuration:方法,目的是animationOn在程序中的其他地方设置我的BOOL 以取消无限循环重复。我的印象completion是每次动画循环结束时都会调用该块,但事实并非如此。

Is the completionblock ever called in a repeating animation? And if not, is there another way I can stop this animation from outside this method?

completion块是否曾在重复动画中被调用?如果没有,有没有另一种方法可以从这种方法之外停止这个动画?

- (void) animateFirst: (UIButton *) button
{
    button.transform = CGAffineTransformMakeScale(1.1, 1.1);
    [UIView animateWithDuration: 0.4
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                     animations: ^{
                         button.transform = CGAffineTransformIdentity;
                     } completion: ^(BOOL finished){
                         if (!animationOn) {
                             [UIView setAnimationRepeatCount: 0];
                         }
    }];
}

回答by Tom van Zummeren

The completion block will only get called when the animation is interrupted. For example it gets called when the app goes in the background and comes back to the foreground again (via multitasking). In that case the animation is stopped. You should restart the animation when that happens.

只有在动画中断时才会调用完成块。例如,当应用程序进入后台并再次返回前台(通过多任务处理)时,它会被调用。在这种情况下,动画会停止。发生这种情况时,您应该重新启动动画。

To stop the animation you can remove it from the view's layer:

要停止动画,您可以将其从视图层中移除:

[button.layer removeAllAnimations];

回答by Recycled Steel

Old but another option.

旧但另一种选择。

You can also setup another animation which is not repeating on the same view, that way you can also capture it in the current state and return it to how it is by using the option UIViewAnimationOptionBeginFromCurrentState. Your completion block is also called.

您还可以设置另一个不在同一视图上重复的动画,这样您也可以在当前状态下捕获它并使用选项 UIViewAnimationOptionBeginFromCurrentState 将其返回到它的状态。您的完成块也被调用。

-(void)someEventSoStop
{
    button.transform = CGAffineTransformMakeScale(1.0, 1.0);
    [UIView animateWithDuration: 0.4
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                     animations: ^{
                         button.transform = CGAffineTransformIdentity;
                     } completion: ^(BOOL finished){

                     }];
}

回答by Luke

I've solved the problem by calling [button.layer removeAllAnimations].

我已经通过调用解决了这个问题[button.layer removeAllAnimations]

回答by Wael Showair

As per the documentation of View class reference: If you used any of the class methods such as animateWithDuration:delay:options:animations:completion:if the duration is set to negative value or 0, the changes are made without performing animation. so I did something like this to stop the infinite animation:

根据 View class reference 的文档:如果您使用了任何类方法,例如animateWithDuration:delay:options:animations:completion:如果持续时间设置为负值或 0,则在不执行动画的情况下进行更改。所以我做了这样的事情来停止无限动画:

[UIView animateWithDuration:0.0 animations:^{
      button.layer.affineTransform = CGAffineTransformIdentity;
  }];

I think this is better than removing all animations from the layer as in the suggested answer. Note that this is applicable for all other class animation methods in the UIView class.

我认为这比在建议的答案中从图层中删除所有动画要好。请注意,这适用于 UIView 类中的所有其他类动画方法。