Andrew
Andrew

Reputation: 16051

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

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?

Upvotes: 5

Views: 4460

Answers (4)

Nishant Mahajan
Nishant Mahajan

Reputation: 264

simply call a method with the delayed time interval

like-

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

Upvotes: -2

Necro
Necro

Reputation: 333

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.

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385600

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

Upvotes: 14

FoJjen
FoJjen

Reputation: 774

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

Upvotes: 14

Related Questions