5

我有这个代码:

[UIView animateWithDuration:0.8
                             animations:^{ 
                                 [self methodToRun];
                             } 
                             completion:^(BOOL finished){
                                 [self anotherMethod];
                             }];

虽然在 methodToRun 中有一些东西,但应用程序不会等待 0.8 秒,而是继续运行另一个方法。有什么办法可以让我在运行第二位之前等待 0.8 秒?

4

2 回答 2

9

Don't misuse an animation block like that. If you really want a delay, use the method Ken linked to or even an easier way is to use GCD (iOS 4+).

Here's an example using a delay of 0.8 like you used in your question:

dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 0.8); 
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
    [self anotherMethod];
});
于 2011-06-13T17:44:21.520 回答
4

您可以添加调用[NSTimer performSelector:withObject:afterDelay]而不是依赖完成参数。

于 2011-06-13T17:35:47.773 回答