SVProgressHUD源码记一:ReadMe解读

作者: One9398 | 来源:发表于2015-12-21 23:03 被阅读4511次

基本使用

SVProgressHUD 为单例对象,不要手动地实例化,直接使用[SVProgressHUD method]方式.

使用场景主要为执行耗时任务时进行遮罩显示或者简单信息的提示来增加用户体验,比如下拉刷新、无限滚动、发送远程消息.

    [SVProgressHUD show]; //显示HUD
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time-consuming task 执行耗时操作(常常为网络访问)
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss]; // 消除HUD
        });
    });

显示HUD

  • 针对任务的执行状态不确定时的HUD显示

    + (void)show;
    + (void)showWithStatus:(NSString*)string;
    
  • 针对任务的执行过程能进度表示的HUD显示

    + (void)showProgress:(CGFloat)progress;
    + (void)showProgress:(CGFloat)progress status:(NSString*)status;
    

销毁HUD

  • 简单销毁

    + (void)dismiss;
    + (void)dismissWithDelay:(NSTimeInterval)delay; // 延时销毁
    
  • 批量HUD销毁

    + (void)popActivity;
    
  • 快速显示信息在HUD销毁后,显示时间0.5~5.0s间

    + (void)showInfoWithStatus:(NSString *)string;
    + (void)showSuccessWithStatus:(NSString*)string;
    + (void)showErrorWithStatus:(NSString *)string;
    + (void)showImage:(UIImage*)image status:(NSString*)string;
    

自定义

+ (void)setDefaultStyle:(SVProgressHUDStyle)style;                  // default is SVProgressHUDStyleLight HUD样式设置
+ (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType;         // default is SVProgressHUDMaskTypeNone HUD背景遮罩层设置
+ (void)setDefaultAnimationType:(SVProgressHUDAnimationType)type;   // default is SVProgressHUDAnimationTypeFlat HUD动画类型设置
+ (void)setMinimumSize:(CGSize)minimumSize;                         // default is CGSizeZero, can be used to avoid resizing for a larger message HUD显示文字信息的最小尺寸
+ (void)setRingThickness:(CGFloat)width;                            // default is 2 pt HUD圆环宽度设置
+ (void)setRingRadius:(CGFloat)radius;                              // default is 18 pt HUD圆环半径设置
+ (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] HUD文本字体设置
+ (void)setForegroundColor:(UIColor*)color;                         // default is [UIColor blackColor], only used for SVProgressHUDStyleCustom HUD前景色设置
+ (void)setBackgroundColor:(UIColor*)color;                         // default is [UIColor whiteColor], only used for  SVProgressHUDStyleCustom HUD背景色设置
+ (void)setInfoImage:(UIImage*)image;                              // default is the bundled info image provided by Freepik HUD内容图片设置
+ (void)setSuccessImage:(UIImage*)image;                            // default is bundled success image from Freepik HUD成功样式的图片设置
+ (void)setErrorImage:(UIImage*)image;                              // default is bundled error image from Freepik HUD错误样式时图片设置
+ (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 HUD销毁的最短时间设置
  • 支持UIAppearance协议方法,能够同一配置.

提示

SVProgressHUD提供两种配置好的样式

  • SVProgressHUDStyleLight: 白色背景,黑色圆环和文字
  • SVProgressHUDStyleDark: 黑色背景,白色圆环和文字
  • 若要自定义必须先设setDefaultStyleSVProgressHUDStyleCustom,再进行setForegroundColor,setBackgroundColor的配置.

通知监听

SVProgressHUD提供了四个通知方法,来对应当HUD出现和销毁时.

  • SVProgressHUDWillAppearNotification 当HUD显示动画开始时
  • SVProgressHUDDidAppearNotification 当HUD已经显示
  • SVProgressHUDWillDisappearNotification 当HUD销毁动画开始时
  • SVProgressHUDDidDisappearNotification 当HUD已经销毁

其通知的userinfo字典存储了HUD的状态,其key
SVProgressHUDStatusUserInfoKey.

SVProgressHUD还有SVProgressHUDDidReceiveTouchEventNotificationSVProgressHUDDidReceiveTouchEventNotification两个通知检测用户对HUD的交互,它们的userinfo为空,但其objectUIEvent对象.

App扩展

在App扩展中使用SVProgressHUD时,需要用到#define SV_APP_EXTENSIONS避免使用到不可用的API,另外在扩展控制器中针对self.view调用setViewForExtension.

相关文章

网友评论

  • 七夜叹:setRingRadius 这个好像不起作用,我只能去改源码里面的半径参数
  • 喵喵夜向黎:真棒,刚好需要
  • 1643cc5a88a2:怎样让hud加到view上呢?
  • 超_iOS:他显示信息时有没有办法自定义显示时间啊,它是根据字数来的
  • 挂着铃铛的兔:[SVProgressHUD showErrorWithStatus:@"网络加载失败"];
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
    sleep(2.);
    dispatch_async(dispatch_get_main_queue(), ^{
    [SVProgressHUD dismiss];
    });
    });
    如果我这样写, 会造成死锁现象,请问这个遇到过吗?
    One9398:@挂着铃铛的兔 没有这样写过.show之后直接使用dispatch_after函数也可以达到延时指定秒数来dismiss提示框的作用,没必要切换线程去dismiss

本文标题:SVProgressHUD源码记一:ReadMe解读

本文链接:https://www.haomeiwen.com/subject/iiwmhttx.html