How do i wait until an animation in a different class is finished before continuing?

无人久伴 提交于 2019-12-12 09:39:32

问题


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?


回答1:


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



回答2:


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



回答3:


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.




回答4:


simply call a method with the delayed time interval

like-

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


来源:https://stackoverflow.com/questions/6330050/how-do-i-wait-until-an-animation-in-a-different-class-is-finished-before-continu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!