Wait for Animation to finish in iOS? [closed]

2019-01-23 22:07发布

I want to execute doSomethingElse after the animation is finished. Another constraint is that the animation code can be of different duration. How can I do that? thanks!

-(void) doAnimationThenSomethingElse {
  [self doAnimation];
  [self doSomethingElse];
}

This for example doesn't work:

animationDuration = 1;
[UIView animateWithDuration:animationDuration
    animations:^{
      [self doAnimation];
    } completion:^(BOOL finished) {
      [self doSomethingElse];
    }
];

5条回答
时光不老,我们不散
2楼-- · 2019-01-23 22:23

When you are not the author of the animation, you can get a callback when the animation ends by using a transaction completion block:

[CATransaction setCompletionBlock:^{
     // doSomethingElse
}];
// doSomething
查看更多
Animai°情兽
3楼-- · 2019-01-23 22:33

From your comments I'd recommend you:

-(void) doAnimation{
    [self setAnimationDelegate:self];
    [self setAnimationDidStopSelector:@selector(finishAnimation:finished:context:)];
    [self doAnimation];
}

- (void)finishAnimation:(NSString *)animationId finished:(BOOL)finished context:(void *)context {
    [self doSomethingElse];
}
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-23 22:35

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

there is the UIView documentation, scroll down to the

Animating Views with Blocks

and

Animating Views

to see hyperlinks that jump you to different parts of the page that explain how to animate views, the ones that you want are under "Animating Views with Blocks" reading the names of the methods makes them self explanatory

查看更多
我命由我不由天
5楼-- · 2019-01-23 22:47

Use block animations:

[UIView animateWithDuration:animationDuration
    animations:^{
        // Put animation code here
    } completion:^(BOOL finished) {
        // Put here the code that you want to execute when the animation finishes
    }
];
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-23 22:49

You need to be able to access the specific instances of the animations you're running in order to coordinate completion actions for each one. In your example, [self doAnimation] doesn't expose us to any animations, so your question cannot be "solved" with what you've provided.

There are few ways to achieve what you want, but it depends on what kind of animations you're dealing with.

As pointed out in other answers, the most common way to execute code after an animation on a view is to pass a completionBlock in: animateWithDuration:completion: Another way to handle property change animations is to set a CATransaction completion block within the scope of the transaction.

However, these particular methods are basically for animating property or hierarchy changes to views. It's the recommended way when your animations involve views and their properties, but it doesn't cover all the kinds of animations you might otherwise find in iOS. From your question, it's not clear as to what kind of animations you're using (or how, or why), but if you are actually touching instances of CAAnimations (a key frame animation or a group of animations), what you'll typically do is set a delegate:

CAAnimation *animation = [CAAnimation animation];
[animation setDelegate:self];
[animatedLayer addAnimation:animation forKeyPath:nil];

// Then implement the delegate method on your class
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    // Do post-animation work here.
}

The point is, how your completion handling is implemented depends on how your animation is implemented. In this case we can't see the latter, so we can't determine the former.

查看更多
登录 后发表回答