NXCategories 0.2.7

NXCategories 0.2.7

测试测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2016年9月

nightx 维护。



  • 作者:
  • nightx

MBProgressHUD

MBProgressHUD 是一个 iOS 嵌入式类,可以在后台线程执行工作时,显示一个半透明的悬停窗口(HUD),其中包含指示器和/或标签。HUD 旨在替换未记录的私有 UIKit UIProgressHUD,同时增加一些额外的功能。

要求

MBProgressHUD 在任何 iOS 版本上都能工作,并且与 ARC 和非 ARC 项目兼容。它依赖于以下 Apple 框架,这些框架应已包含在大多数 Xcode 模板中:

  • Foundation.framework
  • UIKit.framework
  • CoreGraphics.framework

为了构建 MBProgressHUD,需要最新的开发工具。旧版本的 Xcode 可能也能工作,但不会明确维护其兼容性。

将 MBProgressHUD 添加到您的项目

源文件

或者您可以直接添加 MBProgressHUD.hMBProgressHUD.m 源文件到项目中。

  1. 下载最新的代码版本:https://github.com/matej/MBProgressHUD/archive/master.zip,或者将仓库作为 git 子模块添加到您的 git 跟踪项目中。
  2. 在 Xcode 中打开您的项目,然后将 MBProgressHUD.hMBProgressHUD.m 文件拖放到项目中(使用“产品导航器视图”)。如果您在项目外提取了代码存档,请确保选中“复制项目”。
  3. 在任何需要使用 MBProgressHUD 的地方,使用 #import "MBProgressHUD.h" 包含它。

静态库

您也可以将 MBProgressHUD 作为静态库添加到您的项目或工作空间中。

  1. 下载最新的代码版本:https://github.com/matej/MBProgressHUD/downloads,或者将仓库作为 git 子模块添加到您的 git 跟踪项目中。
  2. 在 Xcode 中打开您的项目,然后将 MBProgressHUD.xcodeproj 拖放到项目中或工作空间中(使用“产品导航器视图”)。
  3. 选择您的目标,转到“构建阶段”选项卡。在“链接二进制与库”部分选择添加按钮。在表单中查找并添加 libMBProgressHUD.a。您可能还需要将 MBProgressHUD 添加到“目标依赖”列表中。
  4. 在任何需要使用 MBProgressHUD 的地方,使用 #import <MBProgressHUD/MBProgressHUD.h> 包含它。

用法

在使用 MBProgressHUD 处理长时间运行的任务时,主要遵循的指导原则是保持主线程无工作任务,以便及时更新 UI。因此,推荐的使用 MBProgressHUD 的方法是首先在主线程上设置它,然后将你要执行的任务旋转到新线程上。

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // Do something...
    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";
[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){
    // Do something...
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});

需要注意的是,在上述块内部发出的任何 HUD 更新都将在块完成之前不显示。

有关更多示例,包括如何使用 MBProgressHUD 与异步操作(如 NSURLConnection)一起使用的示例,请参阅附带的演示项目。在头文件(MBProgressHUD.h)中提供了详细的 API 文档。

许可证

此代码是在 MIT 许可证 的条款和条件下发布的。

变更日志

每个 MBProgressHUD 版本的简要总结可以在 wiki 上找到。