ios - MBProgressHUD showAnimated : whileExecutingBlock: not working
<p><p>我正在使用一种扫描蓝牙设备的方法,该方法是从另一个框架导入的。扫描方法需要一段时间,并且会阻塞 GUI,这是我们绝不希望发生的。</p>
<p>我也有 MBProgressHud,试图在扫描时显示一个平视显示器,但它不起作用(平视显示器没有出现)。有什么帮助吗?</p>
<p>这是我目前使用的代码:</p>
<pre><code> [hud showAnimated:YES whileExecutingBlock:^{
self.btDevices = ;
}];
</code></pre>
<p>编辑 1:好的,所以如果我使用这段代码,它仍然会阻塞我的 UI 一段时间,然后突然继续运行。</p>
<pre><code> hud = [ initWithView:self.view];
hud.labelText = @"Now scanning";
hud.dimBackground = YES;
hud.opacity = 0.5;
;
;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
self.btDevices = ;
});
</code></pre>
<p>编辑 2:好的,我将发布我所有的代码块:</p>
<pre><code> hud = [ initWithView:];
hud.labelText = @"Now scanning";
hud.dimBackground = YES;
hud.opacity = 0.5;
[hud showAnimated:YES whileExecutingBlock:^{
self.btDevices = ;
}];
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(), ^{
animated:YES];
});
});
</code></pre>
<hr/>
<pre><code>/** Recursion to get the top most view in view layer. */
- (UIView *)getTopView:(UIView *)view
{
if () {
return ;
}
return view;
}
</code></pre>
<p>我请求在弹出窗口中扫描 bt,但我想在主视图中显示 HUD,所以我编写了一个 block 来检索主视图。也许这就是问题所在?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个:</p>
<p>在您的 viewDidload 或您要放置的方法中</p>
<pre><code>MBProgressHUD *hud = [ initWithView:self.view];
;
;
;
;
;
</code></pre>
<p>还有你的方法</p>
<pre><code>- (void) startScanning {
self.btDevices = ;
}
</code></pre>
<p>或者我认为你应该尝试运行它</p>
<pre><code>dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
// Insert task code here
self.btDevices = ;
});
</code></pre>
<p>用完成 block 试试</p>
<pre><code>[hud showAnimated:YES whileExecutingBlock:^{
self.btDevices = ;
} completionBlock:^{
//code for after completion
}];
</code></pre>
<p>或者你也可以试试这个</p>
<pre><code>;
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 = ;
dispatch_async(dispatch_get_main_queue(), ^{
;
});
});
</code></pre></p>
<p style="font-size: 20px;">关于ios - MBProgressHUD showAnimated : whileExecutingBlock: not working,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30729901/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30729901/
</a>
</p>
页:
[1]