ios - MBProgressHUD showAnimated : whileExecutingBlock: not working

标签 ios objective-c queue mbprogresshud

我正在使用从另一个框架导入的扫描蓝牙设备的方法。扫描方法需要一段时间,并且会阻塞 GUI,这是我们绝对不想发生的事情。

我也有 MBProgressHud,试图在扫描时显示一个 hud,但它不工作(hud 不显示)。有帮助吗?

这是我目前使用的代码:

    [hud showAnimated:YES whileExecutingBlock:^{
          self.btDevices = [Util scanBT];
    }];

编辑 1:好的,所以如果我使用这段代码,它仍然会阻塞我的 UI 一段时间,然后突然间一切都继续运行。

    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.labelText = @"Now scanning";

    hud.dimBackground = YES;
    hud.opacity = 0.5;
    [hud show:YES];
    [hud hide:YES afterDelay:5.0];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
        self.btDevices = [Util scanBT];
    });

编辑 2:好的,我将发布我所有的代码块:

    hud = [[MBProgressHUD alloc] initWithView:[self getTopView:self.view]];
    hud.labelText = @"Now scanning";

    hud.dimBackground = YES;
    hud.opacity = 0.5;

    [hud showAnimated:YES whileExecutingBlock:^{
          self.btDevices = [Util scanBT];
     }];

    dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
    dispatch_async(myqueue, ^{

        //Whatever is happening in the BT scanning method will now happen in the background
        dispatch_async(dispatch_get_main_queue(), ^{

            [MBProgressHUD hideHUDForView:[self getTopView:self.view] animated:YES];
        });
    });

/** Recursion to get the top most view in view layer. */
- (UIView *)getTopView:(UIView *)view
{
    if ([view.superview class]) {
        return [self getTopView:view.superview];
    }

    return view;
}

我请求在弹出窗口中扫描 bt,但我想在主视图中显示 HUD,因此我编写了一个 block 来检索主视图。也许这就是问题所在?

最佳答案

试试这个:

在您的 viewDidload 或您要放置它的方法中

MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [hud setDimBackground:YES];
    [hud setOpacity:0.5f];
    [hud show:YES];
    [hud hide:YES afterDelay:5.0];
 [self performSelector:@selector(startScanning) withObject:nil afterDelay:5.0];

还有你的方法

- (void) startScanning {

    self.btDevices = [Util scanBT];
}

或者我认为你应该尝试运行它

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
    // Insert task code here
self.btDevices = [Util scanBT];
});

用完成 block 试试

[hud showAnimated:YES whileExecutingBlock:^{
       self.btDevices = [Util scanBT];
    } completionBlock:^{
      //code for after completion 
    }];

或者你也可以试试这个

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL);
dispatch_async(myqueue, ^{

    //Whatever is happening in the BT scanning method will now happen in the background
   self.btDevices = [Util scanBT];

    dispatch_async(dispatch_get_main_queue(), ^{

        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

关于ios - MBProgressHUD showAnimated : whileExecutingBlock: not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30729901/

相关文章:

python - 异步队列收集协程的返回值

c - 如果-否则问题

ios - IPA上传错误

ios - 如何使用 iOS8 Social Framework Swift 将视频文件附加到 Publish on Facebook

ios - 是否可以获取推送通知的发送时间戳?

objective-c - 使用 ORSSerialPort 的 arduino/mac 通信问题

php - PHP 中的优先级队列

iphone - 在 FlipsideViewController 中设置 UISwitch 时遇到问题

objective-c - 声明一个 block 方法参数而不使用 typedef

objective-c - 自动调整 NSTableView 的高度