如何取消UIViews基于块的动画?

发布于 2024-12-13 03:18:07 字数 782 浏览 2 评论 0原文

我目前关注以下问题:

我启动一个动画,其中触发了 2 个对象属性。

代码是:

    [UIView animateWithDuration:0.3 animations:^{
        greyscaleImage.alpha     = 1;
        activityIndicator.alpha  = 1;
    } completion:^(BOOL f){
        if(f)
        {
            [activityIndicator startAnimating];
        }
    }];

效果很好。

我发现的唯一问题是,当保存此 ActivityIndi​​cator 和 greyscaleImage 的视图被释放时,我有 0.3 秒的更改使应用程序崩溃。

为了让它更清楚,请想象一个 ViewController,它的视图通过默认的 iOS-modal-View 方式呈现。现在触发该动画,这需要 2 分钟。在到达那 2 分钟之前,您会发现动画非常无聊,并且您想忽略该视图。现在,视图、activityIndi​​cator 和 greyscaleImage 都已释放,动画 o/c 不知道要做什么。

所以我想知道,这里该怎么做 + 为什么调试器指向

  } completion:^(BOOL f){

而不是例如 [activityIndi​​cator ...

有没有办法允许用户在 2 分钟结束之前关闭视图?

此致

i currently focus the following problem:

i start an animation, where 2 objects-attributes are triggered.

the code is:

    [UIView animateWithDuration:0.3 animations:^{
        greyscaleImage.alpha     = 1;
        activityIndicator.alpha  = 1;
    } completion:^(BOOL f){
        if(f)
        {
            [activityIndicator startAnimating];
        }
    }];

which works fine.

the only problem i discovered is, that i have a 0.3 seconds change to crash the app when the view which holds this activityIndicator and greyscaleImage is deallocated.

To make it more clear please imagine a ViewController, its view presented via default iOS-modal-View ways. Now trigger that animation, which takes 2 minutes. before reaching that 2 minutes, you find that animation is quite boring and you want to dismiss that view. now, that the view, activityIndicator and greyscaleImage are released, the animation o/c cannot know what to do.

so i wonder, what to do here + why the debugger points to

  } completion:^(BOOL f){

instead of e.g. [activityIndicator ...

is there a way, to allow user to dismiss the view before the 2 minutes are over?

Best Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情何以堪。 2024-12-20 03:18:07

如果您启动一个需要 0.0 秒的新动画并进入您想要进入的状态,它将取消旧动画并启动新的(即时)“动画”。

例如,当您想要通过转到视图已经所在的位置来停止移动视图时:

[UIView animateWithDuration:0.0
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{movingView.frame = ((CALayer *)movingView.layer.presentationLayer).frame;}
                 completion:^(BOOL finished){}
 ];

options:UIViewAnimationOptionBeginFromCurrentState 很重要。不调用它将使您的动画从上一个动画的结束状态开始。在运动过程中,它会先扭曲到结束位置,然后再扭曲到您希望其停止的位置。即使您的取消“动画”是即时的,来回跳跃也可能是可见的。

注意:动画时间不必是 0.0 秒,任何动画都会取消旧的动画。但不完全确定不同类型的动画。例如,我不知道更改帧是否会阻止褪色。

If you start a new animation that takes 0.0 seconds and goes to the state you want to go to, it will cancel the old one and start the new (instant) 'animation'.

Example for when you want to stop a moving view by going to the place it already is at:

[UIView animateWithDuration:0.0
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{movingView.frame = ((CALayer *)movingView.layer.presentationLayer).frame;}
                 completion:^(BOOL finished){}
 ];

options:UIViewAnimationOptionBeginFromCurrentState is important. Not calling it will let your animation start at the end state of the previous animation. In movement, it would warp to the end location before warping to the place you want it to stop at. Even though your cancel-'animation' is instant, the jumping back and forth may be visible.

Note: The animation time doesn't have to be 0.0 seconds, any animation will cancel the old one. Not entirely sure about different types of animations though. For example, I don't know if changing a frame would stop a fade.

尐籹人 2024-12-20 03:18:07

您可以从视图层中删除所有动画

[movingView.layer removeAllAnimations];

You can remove all the animations from the views layer

[movingView.layer removeAllAnimations];
从此见与不见 2024-12-20 03:18:07

块在启动后始终会完成,并且无法停止(除非您使应用程序崩溃)。您可能想使用通知中心或 NSTimer 来手动更改框架。

Blocks will always complete after they start, and cannot be stopped (unless you crash the app). You might want to use notification center or NSTimer to manually change frames instead.

孤城病女 2024-12-20 03:18:07

设置动画选项 UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction 为我工作。

像这样使用它:

[UIView animateWithDuration:0.5
                          delay:0.0
         usingSpringWithDamping:0.7
          initialSpringVelocity:0
                        options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                    // Do your stuff here, like changing the frame etc.
                    self.containerView.frame = CGRectMake(0, 100, 300, 300);
    } completion:nil];

Setting animation options UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction work for me.

Use it like this:

[UIView animateWithDuration:0.5
                          delay:0.0
         usingSpringWithDamping:0.7
          initialSpringVelocity:0
                        options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                    // Do your stuff here, like changing the frame etc.
                    self.containerView.frame = CGRectMake(0, 100, 300, 300);
    } completion:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文