在继续之前,我如何等待不同类中的动画完成?

And*_*rew 5 iphone objective-c uiviewanimation

我有简单的代码:

[otherClass doMethodOneWhichHasAnimation];
Run Code Online (Sandbox Code Playgroud)

这个动画持续0.8秒.接下来是推送viewcontroller的代码:

SchemeView *schemeView = [[SchemeView alloc] init];
    [self.navigationController pushViewController:schemeView animated:YES];
    [schemeView release];
Run Code Online (Sandbox Code Playgroud)

问题是,它没有等待推动视图控制器,我希望它等待.做延迟只会延迟整个事情,包括第一个动画开始.

如何让它在运行第二位代码之前等待0.8秒?

FoJ*_*jen 14

动画完成后,您可以进行推送

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

          // PushView

        }];
Run Code Online (Sandbox Code Playgroud)

对于<iOS 4+,请查看setAnimationDidStopSelector

要么

[self performSelector:@selector(metohodToPushView) withObject:nil afterDelay:0.8];
Run Code Online (Sandbox Code Playgroud)


rob*_*off 14

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

[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];
Run Code Online (Sandbox Code Playgroud)