1

我正在UIKit根据这个SO answer延迟消息

现在出现了另一个要求,而不仅仅是排队SSHUDView方法调用,我们还应该处理UIAlertView. 例如,一种情况可能是我们显示一个 hud,然后在 1 秒后我们显示另一个 hud,最后在 1 秒后我们显示一个UIAlertView.

现在的问题是,由于SSHUDViews 是在后台线程上异步运行的,所以当我要显示s 时UIAlertViewSSHUDViews 还没有完成显示,所以UIAlertView会覆盖 hud。

基本上我需要一种方法来排队和延迟方法,无论它们是类SSHUDView还是UIAlertView. 反馈队列,您可以在其中延迟单个消息。

4

2 回答 2

1

您所说的听起来非常适合信号量(请参阅标题下使用调度信号量来规范有限资源的使用)!我看到了你链接到的 SO Answer,我认为它不能解决UIView动画的问题。这是我使用信号量的方法。

在您的视图控制器中添加一个实例变量dispatch_semaphore_t _animationSemaphore;并在- init方法中对其进行初始化:

- (id)init
{
  if ((self = [super init])) {
    _animationSemaphore = dispatch_semaphore_create(1);
  }
  return self;
}

(不要忘记在- deallocusing 方法中释放信号量dispatch_release。您可能还想通过 using 等待排队的动画完成dispatch_semaphore_wait,但我会让您自己弄清楚。)

当你想排队一个动画时,你会做这样的事情:

- (void)animateSomething
{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    dispatch_semaphore_wait(_animationSemaphore, DISPATCH_TIME_FOREVER);
    dispatch_async(dispatch_get_main_queue(), ^{
      [UIView animateWithDuration:0.5 animations:^{
        // Your fancy animation code
      } completion:^(BOOL finished) {
        dispatch_semaphore_signal(_animationSemaphore);
      }];
    });
  });
}

您可以使用- animateSomething模板来完成不同的事情,例如显示一个SSHUDView或一个UIAlertView.

于 2012-12-18T16:49:54.553 回答
0

你所描述的听起来像一个动画。为什么不直接使用 UIView 动画并链接一系列动画块:

[UIView animateWithDuration:2
     animations:^{
         // display first HUD
     }
     completion:^(BOOL finished){
         [UIView animateWithDuration:2
              animations:^{
                  // hide first HUD, display second HUD
              }
              completion:^(BOOL finished){
                  [UIView animateWithDuration:2
                       animations:^{
                           // hide second HUD, show UIAlert
                       }
                       completion:nil
                   ];
              }
          ];
     }
 ];
于 2012-12-12T06:32:36.387 回答