我如何等到不同类中的动画完成后再继续?

发布于 2024-11-15 07:28:30 字数 390 浏览 3 评论 0原文

我有简单的代码:

[otherClass doMethodOneWhichHasAnimation];

动画持续 0.8 秒。接下来是推送视图控制器的代码:

SchemeView *schemeView = [[SchemeView alloc] init];
    [self.navigationController pushViewController:schemeView animated:YES];
    [schemeView release];

问题是,它不等待就推送视图控制器,而我希望它等待。延迟只会延迟整个过程,包括第一个动画的开始。

我怎样才能让它在运行第二段代码之前等待 0.8 秒?

I have the simple code of:

[otherClass doMethodOneWhichHasAnimation];

and that animation lasts 0.8 seconds. Followed by code that pushes a viewcontroller:

SchemeView *schemeView = [[SchemeView alloc] init];
    [self.navigationController pushViewController:schemeView animated:YES];
    [schemeView release];

The problem is, it pushes the viewcontroller without waiting, and I'd like it to wait. Doing delay just delays the whole thing, including the first animation from starting.

How can i get it to wait the 0.8 seconds before running the second bit of code?

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

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

发布评论

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

评论(4

清风夜微凉 2024-11-22 07:28:30

您可以在动画完成后推送

[UIView animateWithDuration:0.8
        animations:^{ 
            // Animation
        } 
        completion:^(BOOL finished){

          // PushView

        }];

<< iOS 4+ 看看 setAnimationDidStopSelector

[self performSelector:@selector(metohodToPushView) withObject:nil afterDelay:0.8];

You can push after the animation is done

[UIView animateWithDuration:0.8
        animations:^{ 
            // Animation
        } 
        completion:^(BOOL finished){

          // PushView

        }];

for < iOS 4+ take a look at setAnimationDidStopSelector

or

[self performSelector:@selector(metohodToPushView) withObject:nil afterDelay:0.8];
久光 2024-11-22 07:28:30

您可以使用 CATransaction 在任何动画之后运行完成块,而无需更改创建动画的方法的源代码。您需要链接到QuartzCore框架,并且您需要#import。然后你可以这样做:

[CATransaction begin]; {

    [CATransaction setCompletionBlock:^{
        // This block runs after any animations created before the call to
        // [CATransaction commit] below.  Specifically, if
        // doMethodOneWhichHasAnimation starts any animations, this block
        // will not run until those animations are finished.

        SchemeView *schemeView = [[SchemeView alloc] init];
            [self.navigationController pushViewController:schemeView animated:YES];
            [schemeView release];
    }];

    // You don't need to modify `doMethodOneWhichHasAnimation`.  Its animations are
    // automatically part of the current transaction.
    [otherClass doMethodOneWhichHasAnimation];

} [CATransaction commit];

You can use CATransaction to run a completion block after any animation, without changing the source code of the method that creates the animation. You'll need to link with the QuartzCore framework and you'll need to #import <QuartzCore/QuartzCore.h>. Then you can do this:

[CATransaction begin]; {

    [CATransaction setCompletionBlock:^{
        // This block runs after any animations created before the call to
        // [CATransaction commit] below.  Specifically, if
        // doMethodOneWhichHasAnimation starts any animations, this block
        // will not run until those animations are finished.

        SchemeView *schemeView = [[SchemeView alloc] init];
            [self.navigationController pushViewController:schemeView animated:YES];
            [schemeView release];
    }];

    // You don't need to modify `doMethodOneWhichHasAnimation`.  Its animations are
    // automatically part of the current transaction.
    [otherClass doMethodOneWhichHasAnimation];

} [CATransaction commit];
花开半夏魅人心 2024-11-22 07:28:30

我通过创建一个新类来运行动画来解决这个问题。

此类计算与动画开始同步经过的时间。

您将动画的时间输入到类中,动画块和类计数器都会接收该数字。

当计数器到达末尾时,您知道动画也已完成。

...所有这些都没有随机复杂的库。

I got around this problem by making a new class to run animations.

This class counts how much time as passed synchronized with the start of the animation.

You feed in the time for the animation to the class and both the animation block and the class counter takes that number in.

When the counter has reached its end you know that the animation has also finished.

...All without random complicated Libraries.

唱一曲作罢 2024-11-22 07:28:30

只需调用具有延迟时间间隔的方法,

例如:

[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.00];

simply call a method with the delayed time interval

like-

[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.00];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文