一、简介
SVProgressHUD是简单易用的显示器,用于指示一个持续进行的任务的进度.
项目地址:https://github.com/SVProgressHUD/SVProgressHUD
二、安装
通过Cocoapods
pod 'SVProgressHUD'
手动安装
直接拖进项目里就可以了。
三、使用
SVProgressHUD 作为一个单例(也就是说,它并不需要创建和实例化,你直接调用, 如[SVProgressHUD method]).
使用 SVProgressHUD是非常明智的!常用场景:下拉刷新,无限滚动,发送消息
使用 SVProgressHUD在您的应用程序通常看起来简单:
[SVProgressHUD show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// time-consuming task
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
});
1.显示 HUD
您可以使用下面的方法中的任意一个来显示HUD,以及指示任务的状态:
+ (void)show;
+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType __attribute__((deprecated("Use show and setDefaultMaskType: instead.")));
+ (void)showWithStatus:(NSString*)status;
+ (void)showWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)maskType __attribute__((deprecated("Use showWithStatus: and setDefaultMaskType: instead.")));
typedef NS_ENUM(NSUInteger, SVProgressHUDMaskType) {
SVProgressHUDMaskTypeNone = 1, // default mask type, allow user interactions while HUD is displayed
SVProgressHUDMaskTypeClear, // don't allow user interactions
SVProgressHUDMaskTypeBlack, // don't allow user interactions and dim the UI in the back of the HUD, as on iOS 7 and above
SVProgressHUDMaskTypeGradient, // don't allow user interactions and dim the UI with a a-la UIAlertView background gradient, as on iOS 6
SVProgressHUDMaskTypeCustom // don't allow user interactions and dim the UI in the back of the HUD with a custom color
};
如果您想在HUD指示任务的进度,请使用下列操作之一:
+ (void)showProgress:(float)progress;
+ (void)showProgress:(float)progress maskType:(SVProgressHUDMaskType)maskType __attribute__((deprecated("Use showProgress: and setDefaultMaskType: instead.")));
+ (void)showProgress:(float)progress status:(NSString*)status;
+ (void)showProgress:(float)progress status:(NSString*)status maskType:(SVProgressHUDMaskType)maskType __attribute__((deprecated("Use showProgress:status: and setDefaultMaskType: instead.")));
2.隐藏HUD
HUD可以用以下方法隐藏:
+ (void)dismiss;
+ (void)dismissWithDelay:(NSTimeInterval)delay;
如果你想叠加多个HUD,可以使用以下方法抵消:
+ (void)popActivity;
该HUD将自动消失, popActivity将与显示的次数匹配。
3.显示一个提示消息
也可以用于显示一个提示信息。所述显示时间取决于给定的字符串的长度( 0.5至5秒)和minimumDismissTimeInterval有关。
+ (void)showInfoWithStatus:(NSString *)string;
+ (void)showSuccessWithStatus:(NSString*)string;
+ (void)showErrorWithStatus:(NSString *)string;
+ (void)showImage:(UIImage*)image status:(NSString*)string;
4.自定义
SVProgressHUD 可通过下列方法进行个性化定制:
+ (void)setDefaultStyle:(SVProgressHUDStyle)style; // default is SVProgressHUDStyleLight
+ (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType; // default is SVProgressHUDMaskTypeNone
+ (void)setDefaultAnimationType:(SVProgressHUDAnimationType)type; // default is SVProgressHUDAnimationTypeFlat
+ (void)setContainerView:(UIView*)containerView; // default is window level
+ (void)setMinimumSize:(CGSize)minimumSize; // default is CGSizeZero, can be used to avoid resizing
+ (void)setRingThickness:(CGFloat)width; // default is 2 pt
+ (void)setRingRadius:(CGFloat)radius; // default is 18 pt
+ (void)setRingNoTextRadius:(CGFloat)radius; // default is 24 pt
+ (void)setCornerRadius:(CGFloat)cornerRadius; // default is 14 pt
+ (void)setFont:(UIFont*)font; // default is [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]
+ (void)setForegroundColor:(UIColor*)color; // default is [UIColor blackColor], only used for SVProgressHUDStyleCustom
+ (void)setBackgroundColor:(UIColor*)color; // default is [UIColor whiteColor], only used for SVProgressHUDStyleCustom
+ (void)setBackgroundLayerColor:(UIColor*)color; // default is [UIColor colorWithWhite:0 alpha:0.4], only used for SVProgressHUDMaskTypeCustom
+ (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)setViewForExtension:(UIView*)view; // default is nil, only used if #define SV_APP_EXTENSIONS is set
+ (void)setMinimumDismissTimeInterval:(NSTimeInterval)interval; // default is 5.0 seconds
+ (void)setMaximumDismissTimeInterval:(NSTimeInterval)interval; // default is infinite
+ (void)setFadeInAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setFadeOutAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setMaxSupportedWindowLevel:(UIWindowLevel)windowLevel; // default is UIWindowLevelNormal
Additionally SVProgressHUD supports the UIAppearance protocol for most of the above methods.
5.通知
(1)SVProgressHUD通过 NSNotificationCenter 注册4份通知,以响应正在显示/消失
SVProgressHUDWillAppearNotification 提示框即将出现
SVProgressHUDDidAppearNotification 提示框已经出现
SVProgressHUDWillDisappearNotification 提示框即将消失
SVProgressHUDDidDisappearNotification 提示框已经消失
每个通知传递一个 userInfo字典,字典中包含HUD的状态字符串(如果有的话) ,可通过 SVProgressHUDStatusUserInfoKey作为键来获取。
(2)SVProgressHUD还发送通知:
SVProgressHUDDidReceiveTouchEventNotification当用户触摸整体屏幕上
SVProgressHUDDidTouchDownInsideNotification当用户直接在HUD接触
这两个通知没有 userInfo参数,但包含了有关的触摸的UIEvent参数.
6.提示
As standard SVProgressHUD offers two preconfigured styles:
SVProgressHUDStyleLight: White background with black spinner and text
SVProgressHUDStyleDark: Black background with white spinner and text
If you want to use custom colors use setForegroundColor and setBackgroundColor:. These implicity set the HUD's style to SVProgressHUDStyleCustom.