MBProgressHUD的基本使用
其实MBProgressHUD挺简单的,只有MBProgressHUD.h和.m文件,用的时候拖入工程或使用cocoapods安装(pod 'MBProgressHUD')。想做复杂的效果可以参考作者gitHub上的DEMO(地址:MBProgressHUD),写得也很清楚明了。
//
// MBProgressHUD.h
// Version 1.0.0
/*
进度指示器
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>
@class MBBackgroundView;
@protocol MBProgressHUDDelegate;
extern CGFloat const MBProgressMaxOffset;//设置 MBProgressHUD的位置
/*
MBProgressHUDMode 类型枚举
*/
typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
// 使用UIActivityIndicatorView来显示进度,这是默认值
MBProgressHUDModeIndeterminate,
// 使用一个圆形饼图来作为进度视图
MBProgressHUDModeDeterminate,
// 使用一个水平进度条
MBProgressHUDModeDeterminateHorizontalBar,
// 使用圆环作为进度条
MBProgressHUDModeAnnularDeterminate,
// 显示一个自定义视图,通过这种方式,可以显示一个正确或错误的提示图
MBProgressHUDModeCustomView,
// 只显示文本
MBProgressHUDModeText
};
/*
MBProgressHUDAnimation 动画枚举
*/
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
};
/*
背景样式
*/
typedef NS_ENUM(NSInteger, MBProgressHUDBackgroundStyle) {
/// Solid color background
MBProgressHUDBackgroundStyleSolidColor,
/// UIVisualEffectView or UIToolbar.layer background view
MBProgressHUDBackgroundStyleBlur
};
/*
完成block
*/
typedef void (^MBProgressHUDCompletionBlock)();
#pragma mark ===== 正文 =======
NS_ASSUME_NONNULL_BEGIN
/**
* Displays a simple HUD window containing(包含) a progress indicator(进度指示器) and two optional labels(下面的label,detailsLabel) for short messages.
*
* This is a simple drop-in(插入的) class for displaying a progress HUD view similar to(和...相似) Apple's private (私有的)UIProgressHUD class.
* The MBProgressHUD window spans over the entire space(横跨整个屏幕) given to it by the initWithFrame: constructor and catches all
* user input on this region, thereby preventing the user operations on components below the view.
*
* @note To still allow touches to pass through(穿过…;通过…) the HUD, you can set hud.userInteractionEnabled = NO. 为了不和你的UI冲突,所以这样设置
* @attention MBProgressHUD is a UI class and should therefore only be accessed on the main thread.因为MBProgressHUD继承自UIView,所以必须在主线程上使用
*/
//MBProgressHUD继承自UIView
@interface MBProgressHUD : UIView
#pragma ==== Creates a new HUD And hideHUD ====
/**
* Creates a new HUD(下面是创建a new HUD的方法), adds it to provided view and shows it. The counterpart(对应的人或物; 副本; 对应的人;) to this method is hideHUDForView:animated:.(也就是隐藏的方法)
* 方法的介绍
* @note This method sets removeFromSuperViewOnHide. The HUD will automatically(自动地) be removed from the view hierarchy when hidden.
*
* 参数的介绍
* @param view The view that the HUD will be added to 在哪展示
* @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
* animations while appearing. 动画
* @return A reference to(提及,参考;关于;提到) the created HUD.
*
* @see hideHUDForView:animated:
* @see animationType
*/
+ (instancetype)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
/// @name Showing and hiding
/**
* Finds the top-most HUD subview and hides it. The counterpart to this method is showHUDAddedTo:animated:.
*
* @note This method sets removeFromSuperViewOnHide. The HUD will automatically be removed from the view hierarchy when hidden.
*
* @param view The view that is going to be searched for a HUD subview.
* @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
* animations while disappearing.
* @return YES if a HUD was found and removed, NO otherwise.
*
* @see showHUDAddedTo:animated:
* @see animationType
*/
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;
/**
* Finds the top-most HUD subview and returns it.
*
* @param view The view that is going to be searched.
* @return A reference to the last HUD subview discovered.
*/
+ (nullable MBProgressHUD *)HUDForView:(UIView *)view;
/**
* A convenience constructor(构造函数) that initializes(初始化) the HUD with the view's bounds. Calls the designated constructor with
* view.bounds as the parameter.
*
* @param view The view instance(举...为例) that will provide the bounds for the HUD. Should be the same instance as
* the HUD's superview (i.e., the view that the HUD will be added to).
*/
- (instancetype)initWithView:(UIView *)view;
#pragma ===== Displays the HUD.=====
/**
* Displays the HUD.
*
* @note You need to make sure that the main thread completes its run loop soon after this method call so that
* the user interface can be updated. Call this method when your task is already set up to be executed in a new thread
* (e.g., when using something like NSOperation or making an asynchronous call like NSURLRequest).
*
* @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
* animations while appearing.
*
* @see animationType
*/
- (void)showAnimated:(BOOL)animated;
/**
* Hides the HUD. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
* hide the HUD when your task completes.
*
* @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
* animations while disappearing.
*
* @see animationType
*/
- (void)hideAnimated:(BOOL)animated;
/**
* Hides the HUD after a delay. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
* hide the HUD when your task completes.
*
* @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
* animations while disappearing.
* @param delay Delay in seconds until the HUD is hidden.
*
* @see animationType
*/
- (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay; //带延迟的隐藏
/**
* The HUD delegate object. Receives HUD state notifications.接收HUD的状态通知
*/
@property (weak, nonatomic) id<MBProgressHUDDelegate> delegate;
/**
* Called after the HUD is hiden. 隐藏后调用
*/
@property (copy, nullable) MBProgressHUDCompletionBlock completionBlock;
/*
* Grace period is the time (in seconds) that the invoked method may be run without
* showing the HUD. If the task finishes before the grace time runs out, the HUD will
* not be shown at all.
* This may be used to prevent HUD display for very short tasks.
* Defaults to 0 (no grace time).
*/
// 这个属性设置了一个宽限期,它是在没有显示HUD窗口前被调用方法可能运行的时间。
// 如果被调用方法在宽限期内执行完,则HUD不会被显示。
// 这主要是为了避免在执行很短的任务时,去显示一个HUD窗口。
// 默认值是0。只有当任务状态是已知时,才支持宽限期。具体我们看实现代码。
@property (assign, nonatomic) NSTimeInterval graceTime;
/**
* The minimum time (in seconds) that the HUD is shown.
* This avoids the problem of the HUD being shown and than instantly hidden.
* Defaults to 0 (no minimum show time).
*/
// HUD显示的最短时间。设置这个值是为了避免HUD显示后立即被隐藏。默认值为0
@property (assign, nonatomic) NSTimeInterval minShowTime;
/**
* Removes the HUD from its parent view when hidden.任务完成后从父视图移除
* Defaults to NO.
*/
// 隐藏时是否将HUD从父视图中移除,默认是NO。
@property (assign, nonatomic) BOOL removeFromSuperViewOnHide;
/// @name Appearance
/**
* MBProgressHUD operation mode. The default is MBProgressHUDModeIndeterminate.
*/
//默认情况下,使用的是菊花(MBProgressHUDModeIndeterminate)。
//对于其它几种模式,MBProgressHUD专门我们提供了几个视图类。如果是进度条模式(MBProgressHUDModeDeterminateHorizontalBar),则使用的是MBBarProgressView类;如果是饼图模式(MBProgressHUDModeDeterminate)或环形模式(MBProgressHUDModeAnnularDeterminate),则使用的是MBRoundProgressView类。上面这两个类的主要操作就是在drawRect:中根据一些进度参数来绘制形状,大家可以自己详细看一下。
@property (assign, nonatomic) MBProgressHUDMode mode;
/**
* A color that gets forwarded to all labels and supported indicators. Also sets the tintColor
* for custom views on iOS 7+. Set to nil to manage color individually.
* Defaults to semi-translucent black on iOS 7 and later and white on earlier iOS versions.
*/
@property (strong, nonatomic, nullable) UIColor *contentColor UI_APPEARANCE_SELECTOR;
/**
* The animation type that should be used when the HUD is shown and hidden.
*/
// HUD显示和隐藏的动画类型
@property (assign, nonatomic) MBProgressHUDAnimation animationType UI_APPEARANCE_SELECTOR;
/**
* The bezel offset relative to the center of the view. You can use MBProgressMaxOffset
* and -MBProgressMaxOffset to move the HUD all the way to the screen edge in each direction.
* E.g., CGPointMake(0.f, MBProgressMaxOffset) would position the HUD centered on the bottom edge. 设置HUD弹窗的位置 这个例子会使弹窗从底部弹出
*/
@property (assign, nonatomic) CGPoint offset UI_APPEARANCE_SELECTOR;
/**
* The amount of space between the HUD edge and the HUD elements (labels, indicators or custom views).
* This also represents the minimum bezel distance to the edge of the HUD view.
* Defaults to 20.f
*/
// HUD各元素与HUD边缘的间距
@property (assign, nonatomic) CGFloat margin UI_APPEARANCE_SELECTOR;
/**
* The minimum size of the HUD bezel. Defaults to CGSizeZero (no minimum size).
*/
// HUD背景框的最小大小
@property (assign, nonatomic) CGSize minSize UI_APPEARANCE_SELECTOR;
/**
* Force the HUD dimensions to be equal if possible.
*/
// 是否强制HUD背景框宽高相等
@property (assign, nonatomic, getter = isSquare) BOOL square UI_APPEARANCE_SELECTOR;
/**
* When enabled, the bezel center gets slightly affected by the device accelerometer data.
* Has no effect on iOS < 7.0. Defaults to YES.
*/
@property (assign, nonatomic, getter=areDefaultMotionEffectsEnabled) BOOL defaultMotionEffectsEnabled UI_APPEARANCE_SELECTOR;
/// @name Progress
/**
* The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0.
*/
@property (assign, nonatomic) float progress;
/// @name ProgressObject
/**
* The NSProgress object feeding the progress information to the progress indicator.
*/
@property (strong, nonatomic, nullable) NSProgress *progressObject;
/// @name Views
/**
* The view containing the labels and indicator (or customView).
*/
@property (strong, nonatomic, readonly) MBBackgroundView *bezelView;
/**
* View covering the entire HUD area, placed behind bezelView.
*/
@property (strong, nonatomic, readonly) MBBackgroundView *backgroundView;
/**
* The UIView (e.g., a UIImageView) to be shown when the HUD is in MBProgressHUDModeCustomView.
* The view should implement intrinsicContentSize for proper sizing. For best results use approximately 37 by 37 pixels.
*/
@property (strong, nonatomic, nullable) UIView *customView;
/**
* A label that holds an optional short message to be displayed below the activity indicator. The HUD is automatically resized to fit
* the entire text.
*/
@property (strong, nonatomic, readonly) UILabel *label;
/**
* A label that holds an optional details message displayed below the labelText message. The details text can span multiple lines.
*/
@property (strong, nonatomic, readonly) UILabel *detailsLabel;
/**
* A button that is placed below the labels. Visible only if a target / action is added.
*/
@property (strong, nonatomic, readonly) UIButton *button;
@end
//MBProgressHUD为我们提供了一个HUD窗口的很好的实现,不过个人在使用过程中,觉得它给我们提供的交互功能太少。其代理只提供了一个-hudWasHidden:方法,而且我们也无法通过点击HUD来执行一些操作。在现实的需求中,可能存在这种情况:比如一个网络操作,在发送请求等待响应的过程中,我们会显示一个HUD窗口以显示一个loading框。但如果我们想在等待响应的过程中,在当前视图中取消这个网络请求,就没有相应的处理方式,MBProgressHUD没有为我们提供这样的交互操作。当然这时候,我们可以根据自己的需求来修改源码。
@protocol MBProgressHUDDelegate <NSObject>
@optional
/**
* Called after the HUD was fully hidden from the screen.
*/
- (void)hudWasHidden:(MBProgressHUD *)hud;
@end
#pragma ====== MBRoundProgressView =====
/**
* A progress view for showing definite progress by filling up a circle (pie chart).
*/
@interface MBRoundProgressView : UIView
/**
* Progress (0.0 to 1.0)
*/
@property (nonatomic, assign) float progress;
/**
* Indicator progress color.
* Defaults to white [UIColor whiteColor].
*/
@property (nonatomic, strong) UIColor *progressTintColor;
/**
* Indicator background (non-progress) color.
* Only applicable on iOS versions older than iOS 7.
* Defaults to translucent white (alpha 0.1).
*/
@property (nonatomic, strong) UIColor *backgroundTintColor;
/*
* Display mode - NO = round or YES = annular. Defaults to round.
*/
@property (nonatomic, assign, getter = isAnnular) BOOL annular;
@end
#pragma ======= A flat bar progress view. =====
/**
* A flat bar progress view.
*/
@interface MBBarProgressView : UIView
/**
* Progress (0.0 to 1.0)
*/
@property (nonatomic, assign) float progress;
/**
* Bar border line color.
* Defaults to white [UIColor whiteColor].
*/
@property (nonatomic, strong) UIColor *lineColor;
/**
* Bar background color.
* Defaults to clear [UIColor clearColor];
*/
@property (nonatomic, strong) UIColor *progressRemainingColor;
/**
* Bar progress color.
* Defaults to white [UIColor whiteColor].
*/
@property (nonatomic, strong) UIColor *progressColor;
@end
#pragma ======= MBBackgroundView =====
@interface MBBackgroundView : UIView
/**
* The background style.
* Defaults to MBProgressHUDBackgroundStyleBlur on iOS 7 or later and MBProgressHUDBackgroundStyleSolidColor otherwise.
* @note Due to iOS 7 not supporting UIVisualEffectView, the blur effect differs slightly between iOS 7 and later versions.
*/
@property (nonatomic) MBProgressHUDBackgroundStyle style;
/**
* The background color or the blur tint color.
* @note Due to iOS 7 not supporting UIVisualEffectView, the blur effect differs slightly between iOS 7 and later versions.
*/
// 背景框的颜色
// 需要注意的是如果设置了这个属性,则opacity属性会失效,即不会有半透明效果
@property (nonatomic, strong) UIColor *color;
@end
#pragma ====== MBProgressHUD (Deprecated) =====
@interface MBProgressHUD (Deprecated)
+ (NSArray *)allHUDsForView:(UIView *)view __attribute__((deprecated("Store references when using more than one HUD per view.")));
+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated __attribute__((deprecated("Store references when using more than one HUD per view.")));
- (id)initWithWindow:(UIWindow *)window __attribute__((deprecated("Use initWithView: instead.")));
- (void)show:(BOOL)animated __attribute__((deprecated("Use showAnimated: instead.")));
- (void)hide:(BOOL)animated __attribute__((deprecated("Use hideAnimated: instead.")));
- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay __attribute__((deprecated("Use hideAnimated:afterDelay: instead.")));
- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated __attribute__((deprecated("Use GCD directly.")));
- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block __attribute__((deprecated("Use GCD directly.")));
- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(nullable MBProgressHUDCompletionBlock)completion __attribute__((deprecated("Use GCD directly.")));
- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue __attribute__((deprecated("Use GCD directly.")));
- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue
completionBlock:(nullable MBProgressHUDCompletionBlock)completion __attribute__((deprecated("Use GCD directly.")));
@property (assign) BOOL taskInProgress __attribute__((deprecated("No longer needed.")));
@property (nonatomic, copy) NSString *labelText __attribute__((deprecated("Use label.text instead.")));
@property (nonatomic, strong) UIFont *labelFont __attribute__((deprecated("Use label.font instead.")));
@property (nonatomic, strong) UIColor *labelColor __attribute__((deprecated("Use label.textColor instead.")));
@property (nonatomic, copy) NSString *detailsLabelText __attribute__((deprecated("Use detailsLabel.text instead.")));
@property (nonatomic, strong) UIFont *detailsLabelFont __attribute__((deprecated("Use detailsLabel.font instead.")));
@property (nonatomic, strong) UIColor *detailsLabelColor __attribute__((deprecated("Use detailsLabel.textColor instead.")));
@property (assign, nonatomic) CGFloat opacity __attribute__((deprecated("Customize bezelView properties instead.")));
@property (strong, nonatomic) UIColor *color __attribute__((deprecated("Customize the bezelView color instead.")));
@property (assign, nonatomic) CGFloat xOffset __attribute__((deprecated("Set offset.x instead.")));
@property (assign, nonatomic) CGFloat yOffset __attribute__((deprecated("Set offset.y instead.")));
@property (assign, nonatomic) CGFloat cornerRadius __attribute__((deprecated("Set bezelView.layer.cornerRadius instead.")));
@property (assign, nonatomic) BOOL dimBackground __attribute__((deprecated("Customize HUD background properties instead.")));
@property (strong, nonatomic) UIColor *activityIndicatorColor __attribute__((deprecated("Use UIAppearance to customize UIActivityIndicatorView. E.g.: [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor redColor];")));
@property (atomic, assign, readonly) CGSize size __attribute__((deprecated("Get the bezelView.frame.size instead.")));
@end
NS_ASSUME_NONNULL_END
其他人已经总结的很好了,就不重复造车了。所以MBProgressHUD的基本使用,可参考以下地址(有的方法已经Deprecated,所以仅为参考):
iOS 源代码分析 --- MBProgressHUD: http://www.cocoachina.com/ios/20160601/16552.html
MBProgressHUD的基本使用: https://my.oschina.net/jilin/blog/393005
MBProgressHUD 使用详解:http://m.blog.csdn.net/article/details?id=51208310
posted on 2016-11-10 23:26 dreamDeveloper 阅读(1444) 评论(0) 编辑 收藏 举报