我正在使用一种扫描蓝牙设备的方法,该方法是从另一个框架导入的。扫描方法需要一段时间,并且会阻塞 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-推荐答案 strong>
试试这个:
在您的 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/
|