开发者

UIView animation block not waiting

开发者 https://www.devze.com 2023-03-13 04:06 出处:网络
I have this code: [UIView animateWithDuration:0.8 animations:^{ [self methodToRun]; } completion:^(BOOL finished){

I have this code:

[UIView animateWithDuration:0.8
                             animations:^{ 
                                 [self methodToRun];
                             } 
                             completion:^(BOOL finished){
                                 [self anotherMethod];
  开发者_开发知识库                           }];

Although there's things in methodToRun, the app just doesn't wait the 0.8 seconds and proceeds to run anotherMethod. Is there any way I can simply just get to wait 0.8 seconds before running the second bit?


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


You could add a call to [NSTimer performSelector:withObject:afterDelay] instead of relying on the completion parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消