SVProgressHUD
是一个简洁易用的 HUD,用于显示正在进行的任务进度。
SVProgressHUD/SVProgressHUD
文件夹拖放到您的项目中。SVProgressHUD.bundle
添加到 Targets -> Build Phases -> Copy Bundle Resources
中。(见 /Demo
中的示例 Xcode 项目)
SVProgressHUD
作为单一实例创建(即不需要显式分配和实例化;直接调用 [SVProgressHUD method]
)。
请合理使用 SVProgressHUD
!仅在有绝对必要在前进向用户之前执行任务时使用。错误使用示例:下拉刷新、无限滚动、发送消息。
在您的应用中使用 SVProgressHUD
通常如下所示(使用 Grand Central Dispatch)
[SVProgressHUD show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// time-consuming task
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
});
您可以使用以下选项之一显示不确定任务的进度
+ (void)show;
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType;
+ (void)showWithStatus:(NSString*)string;
+ (void)showWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)maskType;
如果您想使 HUD 反映任务进度,请使用以下之一
+ (void)showProgress:(CGFloat)progress;
+ (void)showProgress:(CGFloat)progress status:(NSString*)status;
+ (void)showProgress:(CGFloat)progress status:(NSString*)status maskType:(SVProgressHUDMaskType)maskType;
可以通过以下方式立即隐藏 HUD:
+ (void)dismiss;
如果您想堆叠 HUD,可以通过以下方式平衡每个显示调用:
+ (void)popActivity;
当 popActivity
调用次数匹配显示调用次数时,HUD 将被隐藏。
或者显示一个确认图标,稍后隐藏。显示时间取决于给定字符串的长度(介于 0.5 到 5 秒之间)。
+ (void)showInfoWithStatus:(NSString *)string;
+ (void)showInfoWithStatus:(NSString *)string maskType:(SVProgressHUDMaskType)maskType;
+ (void)showSuccessWithStatus:(NSString*)string;
+ (void)showSuccessWithStatus:(NSString*)string maskType:(SVProgressHUDMaskType)maskType;
+ (void)showErrorWithStatus:(NSString *)string;
+ (void)showErrorWithStatus:(NSString *)string maskType:(SVProgressHUDMaskType)maskType;
+ (void)showImage:(UIImage*)image status:(NSString*)string;
+ (void)showImage:(UIImage*)image status:(NSString*)status maskType:(SVProgressHUDMaskType)maskType;
SVProgressHUD
可以通过以下方法进行自定义
+ (void)setBackgroundColor:(UIColor*)color; // default is [UIColor whiteColor]
+ (void)setForegroundColor:(UIColor*)color; // default is [UIColor blackColor]
+ (void)setRingThickness:(CGFloat)width; // default is 4 pt
+ (void)setFont:(UIFont*)font; // default is [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]
+ (void)setInfoImage:(UIImage*)image; // default is the bundled info image provided by Freepik
+ (void)setSuccessImage:(UIImage*)image; // default is bundled success image from Freepik
+ (void)setErrorImage:(UIImage*)image; // default is bundled error image from Freepik
+ (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType; // default is SVProgressHUDMaskTypeNone
+ (void)setViewForExtension:(UIView*)view; // default is nil, only used if #define SV_APP_EXTENSIONS is set
SVProgressHUD
通过 NSNotificationCenter
发布四个通知来响应显示/隐藏操作
SVProgressHUDWillAppearNotification
在显示动画开始时SVProgressHUDDidAppearNotification
在显示动画完成后SVProgressHUDWillDisappearNotification
在隐藏动画开始时SVProgressHUDDidDisappearNotification
在隐藏动画完成后每个通知都会传递一个包含HUD状态字符串(如果有的话)的userInfo
字典,可以通过SVProgressHUDStatusUserInfoKey
获取。
SVProgressHUD
还发布SVProgressHUDDidReceiveTouchEventNotification
通知,当用户触摸整个屏幕时,或者当用户直接触摸HUD时发布SVProgressHUDDidTouchDownInsideNotification
通知。对于这些通知,不传递userInfo
,但对象参数包含相关的UIEvent
触摸事件。
在App扩展中使用SVProgressHUD
时,请使用#define SV_APP_EXTENSIONS
以避免使用不可用的API。另外,从您的扩展视图控制器中调用setViewForExtension:
并传递self.view
。
如果您有功能请求或错误报告,请通过发送pull请求或创建新问题来帮助。请花些时间来查看由Nicolas Gallagher编写的指导方针。
SVProgressHUD
是由Sam Vermette和项目的贡献者共同完成的。如果您在项目中使用了SVProgressHUD
,会非常欣赏您的贡献。info,success和error图标由Freepik和Flaticon提供,并授权在Creative Commons BY 3.0下。