首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewAnimation从dispatch_after阻塞UIViewAnimation完成块

UIViewAnimation从dispatch_after阻塞UIViewAnimation完成块
EN

Stack Overflow用户
提问于 2014-04-29 11:55:11
回答 2查看 615关注 0票数 0

我有以下设想:

代码语言:javascript
复制
dispatch_after_delta(0.1, ^{
    [self checkForTodaysBonus];  // It contains animation methods.
});

代码语言:javascript
复制
-(void) checkForTodaysBonus {
     // Prepare View and other data and then animate UIView
     [Animations moveDown:self.view andAnimationDuration:0.3 andWait:YES andLength:self.view.frame.size.height];
}

其中,moveDown方法类似于:

代码语言:javascript
复制
+ (void) moveDown: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
    __block BOOL done = wait; //wait =  YES wait to finish animation
    [UIView animateWithDuration:duration animations:^{
        view.center = CGPointMake(view.center.x, view.center.y + length);

    } completion:^(BOOL finished) {
         // This never happens if I call this method from dispatch_after. 
         done = NO;
    }];
    // wait for animation to finish
    // This loop will allow wait to complete animation
      while (done == YES) {  // Application unable to break this condition
         [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
       }
}

代码语言:javascript
复制
 void dispatch_after_delta(float delta, dispatch_block_t block){
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta * NSEC_PER_SEC), dispatch_get_main_queue(), block);
 }

因此,发出,每当从dispatch_after_delta调用animation方法时,animation方法never gets its completion block

什么是可能的解决办法?

EN

回答 2

Stack Overflow用户

发布于 2014-04-29 13:51:11

我给你的建议是使用performSelector: withObject: afterDelay:

将当前的dispatch_after替换为:

代码语言:javascript
复制
[self performSelector:@selector(checkForTodaysBonus) withObject:nil afterDelay:1.0f];
票数 0
EN

Stack Overflow用户

发布于 2014-12-24 03:26:15

而不是因为你提交了块

代码语言:javascript
复制
^{
   [self checkForTodaysBonus];  // It contains animation methods.
});  

对于主队列,并且主队列是一个串行队列,所以动画完成块要到上面的块返回时才会执行。

要解决这个问题,您可以:

  1. 将块提交到全局队列 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block);
  2. 在主队列中执行动画: dispatch_async(dispatch_get_main_queue(),^{ UIView动画:持续时间动画:^ view.center =CGPointMake(view.cent.x,view.cent.y+ length);}完成:^(BOOL已完成){ //如果我从dispatch_after调用此方法,则永远不会发生这种情况。已完成=否;}; });

在我看来,最好不要在分派块中显式地使用NSThread和NSRunLoop。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23363970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档