源码阅读及解析源码阅读及剖析iOS-学习笔记

MBProgressHUD详解(一)

2016-04-21  本文已影响2269人  凌巅

MBProgressHUD详解(一)

MBProgressHUD是一个显示HUD窗口的第三方类库,源码点击这里
它主要用于在执行一些后台任务时,在程序中显示一个表示进度的loading视图和两个可选的文本提示的HUD窗口。大多时候用在加载网络数据的时候,这个库几乎成为APP的标配,试问有那个APP没有网络通信,没有网络交互。按照github上的实例说明,基本都够用了。本文主要是作为学习笔记,从源码层面分析一下这个库的实现,就源码来说,就两个文件一个头文件(MBProgressHUD.h),一个实现文件(MBProgressHUD.m),库本身并没有用到什么高深的技术,简单既是美!很适合初学者学习。
</br>我们先看一下头文件的内容

MBProgressHUD.h

前一段代码MBProgressHUD定义了一些HUD所需的枚举,具体的信息可以看一下注释,就不多做介绍了,下面主要部分也是主要以代码注释说明为主

//MBProgressHUD进度条的模式,即样式
typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
    /// UIActivityIndicatorView.  
    //默认使用UIActivityIndicatorView来显示进度,由系统提供
    MBProgressHUDModeIndeterminate,
    /// A round, pie-chart like, progress view. 
    //圆形饼图进度视图
    MBProgressHUDModeDeterminate,
    /// Horizontal progress bar. 
    //水平进度条
    MBProgressHUDModeDeterminateHorizontalBar,
    /// Ring-shaped progress view.
    //圆环进度条
    MBProgressHUDModeAnnularDeterminate,
    /// Shows a custom view.
    //自定义视图,可以自定义提示图
    MBProgressHUDModeCustomView,
    /// Shows only labels.
    //只显示文本
    MBProgressHUDModeText
};
//MBProgressHUD进度条的动画效果
typedef NS_ENUM(NSInteger, MBProgressHUDAnimation) {
    /// Opacity animation
    //不透明
    MBProgressHUDAnimationFade,
    /// Opacity + scale animation (zoom in when appearing zoom out when disappearing)
    //出现和消失时,透明变化
    MBProgressHUDAnimationZoom,
    /// Opacity + scale animation (zoom out style)
    //消失时透明效果变化
    MBProgressHUDAnimationZoomOut,
    /// Opacity + scale animation (zoom in style)
    //出现时透明效果变化
    MBProgressHUDAnimationZoomIn
};
//MBProgressHUD进度条的背景样式
typedef NS_ENUM(NSInteger, MBProgressHUDBackgroundStyle) {
    MBProgressHUDBackgroundStyleSolidColor,
    MBProgressHUDBackgroundStyleBlur
};

</br></br>
接下来我们看一下MBProgressHUD提供的一些属性和函数,同样以代码注释说明为主

@interface MBProgressHUD : UIView
//创建一个新的HUD,并把它显示在view之上,还可以设置是否以动画的形式,此外,该函数返回一个HUD的对象
//默认removeFromSuperViewOnHide属性为YES
+ (instancetype)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;

//找到最上层的HUD subview 并把它隐藏,成功为YES、其他情况为NO
//同时置removeFromSuperViewOnHide = YES
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;

//返回最上层的HUD subview
+ (nullable MBProgressHUD *)HUDForView:(UIView *)view;

//以view为基准创建初始化一个HUD对象,为HUD的初始化构造函数
- (instancetype)initWithView:(UIView *)view;

//显示HUD控件,此函数应该在主线程中调用
- (void)showAnimated:(BOOL)animated;

//隐藏HUD控件,animated控制是否显示动画。对应于- (void)showAnimated:(BOOL)animated;
- (void)hideAnimated:(BOOL)animated;

//在delay时间之后隐藏HUD,animated控制显示动画与否,delay控制延迟时间
- (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay;

//MBProgressHUDDelegate代理
@property (weak, nonatomic) id<MBProgressHUDDelegate> delegate;

//宽限时间,在graceTime之后才显示HUD。此值默认是0,直接显示
@property (assign, nonatomic) NSTimeInterval graceTime;

//HUD最小显示时间,在执行短时任务时可以设置这个值控制HUD的显示时间
@property (assign, nonatomic) NSTimeInterval minShowTime;

//当HUD隐藏时 是否从父视图中移除HUD对象
@property (assign, nonatomic) BOOL removeFromSuperViewOnHide;

//MBProgressHUD显示样式
@property (assign, nonatomic) MBProgressHUDMode mode;

//MBProgressHUD显示内容的颜色--(待定)
@property (strong, nonatomic, nullable) UIColor *contentColor UI_APPEARANCE_SELECTOR;

//MBProgressHUD动画效果
@property (assign, nonatomic) MBProgressHUDAnimation animationType UI_APPEARANCE_SELECTOR;

//挡板相对于视图中心的偏移
@property (assign, nonatomic) CGPoint offset UI_APPEARANCE_SELECTOR;

//HUD边缘和HUD元素之间的空间大小
@property (assign, nonatomic) CGFloat margin UI_APPEARANCE_SELECTOR;

//HUD的bezel的最小大小
@property (assign, nonatomic) CGSize minSize UI_APPEARANCE_SELECTOR;

//是否强制HUD背景框宽高相等
@property (assign, nonatomic, getter = isSquare) BOOL square UI_APPEARANCE_SELECTOR;

//当启用时,bezel的中心会被设备的加速计数据影响,iOS < 7.0一下可忽略
@property (assign, nonatomic, getter=areDefaultMotionEffectsEnabled) BOOL defaultMotionEffectsEnabled UI_APPEARANCE_SELECTOR;

//进度指标条的当前进入 数值为(0.0-1.0)
@property (assign, nonatomic) float progress;

//小方块背景视图
@property (strong, nonatomic, readonly) MBBackgroundView *bezelView;

//HUD整个的背景视图,放置在bezelView之后
@property (strong, nonatomic, readonly) MBBackgroundView *backgroundView;

//当HUD是MBProgressHUDModeCustomView的模式时,所显示的指示器图片,这个大小最好是37*37像素
@property (strong, nonatomic, nullable) UIView *customView;

//指示器下方的lebel控件,显示简要信息
@property (strong, nonatomic, readonly) UILabel *label;

//指示器下方的lebel控件,显示详细信息
@property (strong, nonatomic, readonly) UILabel *detailsLabel;

//labels下方的button按钮,仅当添加目标/动作时可见
@property (strong, nonatomic, readonly) UIButton *button;

@end

前面说到,MBProgressHUD有一个mode属性,这个属性可以控制HUD的窗口模式,默认是使用系统提供的菊花型进度视图,我们可以通过这个属性来改变窗口模式,这里提供了两个类来实现自定义的进度条样式,圆形视图[圆饼或者圆环](MBRoundProgressView)、棒状视图(MBBarProgressView)。同时也提供了几个公有属性供使用者来修改外观,见代码注释

//一个圆形进度条视图
@interface MBRoundProgressView : UIView 

//进度条当前数值大小
@property (nonatomic, assign) float progress;

//指示器进度条颜色
@property (nonatomic, strong) UIColor *progressTintColor;

//进度条背景颜色
@property (nonatomic, strong) UIColor *backgroundTintColor;

//进度条形状,NO是圆  YES是圆环
@property (nonatomic, assign, getter = isAnnular) BOOL annular;

@end


//扁平棒状进度条视图
@interface MBBarProgressView : UIView

//进度条当前数值大小
@property (nonatomic, assign) float progress;

//进度条边框线的颜色
@property (nonatomic, strong) UIColor *lineColor;

//进度条背景颜色
@property (nonatomic, strong) UIColor *progressRemainingColor;

//进度条颜色
@property (nonatomic, strong) UIColor *progressColor;

@end

MBProgressHUD提供了一个简单的背景视图View,我们可以设置背景视图样式,这个属性我们应该不怎么常用

//MBProgressHUD的背景视图
@interface MBBackgroundView : UIView

//背景视图样式
//iOS 7或者以上版本默认风格是MBProgressHUDBackgroundStyleBlur,其他为MBProgressHUDBackgroundStyleSolidColor
//注意由于iOS 7不支持UIVisualEffectView,所以在iOS7 和 更高版本中会有所不同
@property (nonatomic) MBProgressHUDBackgroundStyle style;

//背景颜色
//注意由于iOS 7不支持UIVisualEffectView,所以在iOS7 和 更高版本中会有所不同
@property (nonatomic, strong) UIColor *color;

@end

</br>
MBProgressHUD还为我们提供了一个代理MBProgressHUDDelegate,这个代理中只提供了一个方法,即:

//代理类
@protocol MBProgressHUDDelegate <NSObject>

@optional
//当HUD完全消失后调用
- (void)hudWasHidden:(MBProgressHUD *)hud;
@end

这个代理方法是在隐藏HUD窗口后调用,如果此时我们需要在我们自己的实现中执行某些操作,则可以实现这个方法。

这篇文章主要简略介绍一下MBProgressHUD的头文件,(枚举,函数,属性,以及所用到的视图类),下一篇我们主要讲一下,HUD对象的工作过程,以及这些属性,函数,视图怎么影响HUD的最终显示效果

上一篇下一篇

猜你喜欢

热点阅读