·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> MBProgressHUD优雅地去提示

MBProgressHUD优雅地去提示

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

项目主页: MBPRogressHUD

实例下载: 点击下载

快速上手:

当执行需要较长时间的任务时,使用MBProgressHUD最重要的一点是: 保证主线程是空闲的,这样可以使UI实时更新.因此: 建议在 主线程中使用 MBProgressHUD, 把其他你想要执行的任务放到其他的线程里:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // 执行某些任务...
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

如果你想配置 HUD,你可以使用由 showHUDAddedTo:animated: 方法返回的 MBProgressHUD 的实例.

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = @"Loading";

// doSomethngInBackgroudWithProgressCallback: 代指某个和进度有关的 自定义方法.
[self doSomethingInBackgroundWithProgressCallback:^(float progress) {
    hud.progress = progress;
} completionCallback:^{
    [hud hide:YES];
}];

UI 更新,应该通常总是在主线程完成.但是某些 MBProgressHUD 设置器,考虑到了”线程安全”,并且可以在后台线程里被调用.这些设置器,具体指的是: setMode:,setCustomView:setLabelText:setLabelFont:setDetailsLabelText:,setDetailsLabelFont: 和 setProgress:.

如果你需要在主线程执行需要长时间运行的任务,你应该在短暂的延迟后再执行这个任务,这样在你的任务阻塞主线程之前, UIKit 就有足够的时间来更新UI(如,绘制HUD).

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // 执行某个 耗时较长的操作.
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});