MBProgressHUD是iOS中用的比较多的开源库(点此下载)。它主要是用来做提示作用的。如下图所示:
添加MBProgressHUD到你的工程中
有三种方法:
使用
初始化hud:- (id)initWithView:(UIView *)view;
- (id)initWithWindow:(UIWindow *)window;
如上所示,有两个方法,不过我觉得第二个和第一个是重复的,因为window本来就是一种特殊的UIView。这个方法初始化了一个MBProgressHUD(以下简称hud)。大小就是view的bounds,注意hud不是中间那个小的指示标志,而是一个大的view,包含了中间那个小的指示标识。
除了上面两个方法,还有个类方法也可创建hud,并显示:
+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
隐藏也有个类似的方法:
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;
代理:
hud有个代理方法:
/**
* Called after the HUD was fully hidden from the screen.
*/
- (void)hudWasHidden:(MBProgressHUD *)hud;
上面的解释说得很清楚了,在hud隐藏之后,就会触发这个代理方法。你可以在hud隐藏之后做需要做得事情。
显示:
- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated;
这个方法会在hud显示后在一个新的线程中执行后台任务(method)。
属性
@property (copy) NSString *labelText;
如第二张图所示“loading”提示文字。
@property (copy) NSString *detailsLabelText;
如第三张图中所示"updating data"提示文字。
/**
* Force the HUD dimensions to be equal if possible.
*/
@property (assign, getter = isSquare) BOOL square;
强制hud中间的黑色半透明指示为正方形(文本提示增加,可导致半透明指示发生形状变化)。
/**
* The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0.
*/
@property (assign) float progress;
进度指示,从0到1.默认0.
/**
* MBProgressHUD operation mode. The default is MBProgressHUDModeIndeterminate.
*
* @see MBProgressHUDMode
*/
@property (assign) MBProgressHUDMode mode;
外观模式,默认为MBProgressHUDModeIndeterminate模式。
模式有以下几种:
typedef enum {
/** Progress is shown using an UIActivityIndicatorView. This is the default. */
MBProgressHUDModeIndeterminate,
/** Progress is shown using a round, pie-chart like, progress view. */
MBProgressHUDModeDeterminate,
/** Progress is shown using a horizontal progress bar */
MBProgressHUDModeDeterminateHorizontalBar,
/** Progress is shown using a ring-shaped progress view. */
MBProgressHUDModeAnnularDeterminate,
/** Shows a custom view */
MBProgressHUDModeCustomView,
/** Shows only labels */
MBProgressHUDModeText
} MBProgressHUDMode;
/**
* The minimum size of the HUD bezel. Defaults to CGSizeZero (no minimum size).
*/
@property (assign) CGSize minSize;
半透明指示最小尺寸。
/**
* Cover the HUD background view with a radial gradient.
*/
@property (assign) BOOL dimBackground;
背景呈现渐变梯度。
@property (MB_STRONG) UIColor *color;
指示标志颜色。
/**
* The amount of space between the HUD edge and the HUD elements (labels, indicators or custom views).
* Defaults to 20.0
*/
@property (assign) float margin;
指示标志中元素(label,customview)与指示标志间距。
/**
* The y-axis offset of the HUD relative to the centre of the superview.
*/
@property (assign) float yOffset;
指示标志中心y坐标与父视图center的y坐标差值,即指示标志y方向偏移量。整数往下偏,反之往上。
/**
* Removes the HUD from its parent view when hidden.
* Defaults to NO.
*/
@property (assign) BOOL removeFromSuperViewOnHide;
hud隐藏时是否从父视图移除。
方法
- (void)show:(BOOL)animated;
- (void)hide:(BOOL)animated;
- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
显示与隐藏。
hud还支持block:
- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block;
- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(MBProgressHUDCompletionBlock)completion;