• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - MBProgressHUD showAnimated : whileExecutingBlock: not working

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:56:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我也有 MBProgressHud,试图在扫描时显示一个平视显示器,但它不起作用(平视显示器没有出现)。有什么帮助吗?

这是我目前使用的代码:

    [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 *)getTopViewUIView *)view
{
    if ([view.superview class]) {
        return [self getTopView:view.superview];
    }

    return view;
}

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



Best Answer-推荐答案


试试这个:

在您的 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 performSelectorselector(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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap