MBProgressHUD 使用教程
MBProgressHUDMBProgressHUD + Customizations项目地址:https://gitcode.com/gh_mirrors/mb/MBProgressHUD
项目介绍
MBProgressHUD 是一个为 iOS 应用添加半透明 HUD(Head-Up Display)的第三方框架。它可以在后台任务执行时显示一个带有指示器和/或标签的半透明 HUD,旨在替代 UIKit 中未公开的 UIProgressHUD,并提供一些额外的功能。MBProgressHUD 适用于 iOS 9.0 及以上版本,依赖于 Foundation、UIKit 和 CoreGraphics 框架。
项目快速启动
安装 MBProgressHUD
使用 CocoaPods
在你的 Podfile
中添加以下内容:
pod 'MBProgressHUD', '~> 1.2.0'
然后运行 pod install
。
使用 Carthage
在你的 Cartfile
中添加以下内容:
github "jdg/MBProgressHUD" ~> 1.2.0
然后运行 carthage update
,并按照 Carthage 的标准安装指南将 MBProgressHUD 添加到你的项目中。
基本使用
在你的代码中导入 MBProgressHUD:
#import "MBProgressHUD.h"
显示 HUD:
[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:
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"加载中...";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// 执行后台任务
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
});
显示完成消息
在任务完成后显示一条完成消息:
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"完成";
hud.mode = MBProgressHUDModeText;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// 执行后台任务
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES afterDelay:1.5];
});
});
典型生态项目
MBProgressHUD 作为一个常用的 HUD 显示框架,广泛应用于各种 iOS 应用中,尤其是需要长时间后台任务处理的应用。它简单易用,且提供了丰富的自定义选项,使得开发者可以根据需要灵活调整 HUD 的显示效果。
相关项目
- SVProgressHUD:另一个流行的 HUD 显示框架,提供了类似的功能。
- JGProgressHUD:一个功能更丰富的 HUD 框架,支持更多的自定义选项和动画效果。
通过这些项目,开发者可以更好地理解和掌握 HUD 的使用,提升用户体验。
MBProgressHUDMBProgressHUD + Customizations项目地址:https://gitcode.com/gh_mirrors/mb/MBProgressHUD