這里有源碼分析:http://www.itdecent.cn/p/485b8d75ccd4
源碼筆記---MBProgressHUD 不在累贅。
說(shuō)明:這篇文章主要是自己開(kāi)發(fā)的時(shí)候查閱使用,參考了許多的資料。
1. 單例擴(kuò)展:
參考 https://github.com/hungryBoy/alertView;
#import <Foundation/Foundation.h>
#import "MBProgressHUD.h"
static NSString *const kLoadingMessage = @"加載中";
static CGFloat const kShowTime = 2.0f;
@interface MBManager : NSObject
/**
* 是否顯示變淡效果,默認(rèn)為YES, PS:只為 showPermanentAlert:(NSString *) alert 和 showLoading 方法添加
*/
@property (nonatomic, assign) BOOL isShowGloomy;
/**
* 顯示“加載中”,待圈圈,若要修改直接修改kLoadingMessage的值即可
*/
+ (void) showLoading;
/**
* 一直顯示自定義提示語(yǔ),不帶圈圈
*
* @param alert 提示信息
*/
+ (void) showPermanentAlert:(NSString *) alert;
/**
* 顯示簡(jiǎn)短的提示語(yǔ),默認(rèn)2秒鐘,時(shí)間可直接修改kShowTime
*
* @param alert 提示信息
*/
+ (void) showBriefAlert:(NSString *) alert;
/**
* 隱藏alert
*/
+(void)hideAlert;
///**
// * 自定義加載視圖接口,支持自定義圖片
// *
// * @param imageName 要顯示的圖片,最好是37 x 37大小的圖片
// * @param title 要顯示的提示文字
// */
//+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title;
/***************************************
* *
* 以下方法根據(jù)情況可選擇使用,一般使用不到 *
* *
***************************************
*/
/**
* 顯示簡(jiǎn)短提示語(yǔ)到view上
*
* @param message 提示語(yǔ)
* @param view 要添加到的view
*/
+ (void) showBriefMessage:(NSString *) message InView:(UIView *) view;
/**
* 顯示長(zhǎng)久的(只要不用手觸摸屏幕或者調(diào)用hideAlert方法就會(huì)一直顯示)提示語(yǔ)到view上
*
* @param message 提示語(yǔ)
* @param view 要添加到的view
*/
+ (void) showPermanentMessage:(NSString *)message InView:(UIView *) view;
/**
* 顯示網(wǎng)絡(luò)加載到view上
*
* @param view 要添加到的view
*/
+ (void) showLoadingInView:(UIView *) view;
/**
* 自定義加載視圖接口,支持自定義圖片
*
* @param imageName 要顯示的圖片,最好是37 x 37大小的圖片
* @param title 要顯示的提示文字
* @param view 要把提示框添加到的view
*/
+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title inView:(UIView *)view;
@end
- 特性
1.對(duì)MBProgressHUD就行封裝,調(diào)用只需要一句代碼
2.添加手勢(shì)功能,觸摸屏幕就可以取消提示框
#import "MBManager.h"
#define kScreen_height [[UIScreen mainScreen] bounds].size.height
#define kScreen_width [[UIScreen mainScreen] bounds].size.width
@interface MBManager ()<UIGestureRecognizerDelegate>
{
UITapGestureRecognizer *tap;
}
@end
@implementation MBManager
UIView *bottomView;
static MBManager *hudManager = nil;
UIView *hudAddedView;
#pragma mark - 初始化
-(instancetype)init{
if (self = [super init]) {
[self initBackView];
self.isShowGloomy = YES;
}
return self;
}
#pragma mark - 初始化深色背景
-(void)initBackView{
bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
bottomView.backgroundColor = [UIColor blackColor];
bottomView.alpha = 0.5;
bottomView.hidden = YES;
}
#pragma mark - 單例
+(instancetype )shareManager{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
hudManager = [[self alloc] init];
});
return hudManager;
}
#pragma mark - 簡(jiǎn)短提示語(yǔ)
+ (void) showBriefMessage:(NSString *) message InView:(UIView *) view{
hudAddedView = view;
[self shareManager];
if (view == nil) {
view = [[UIApplication sharedApplication] windows].lastObject;
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.labelText = message;
hud.mode = MBProgressHUDModeText;
hud.margin = 10.f;
//HUD.yOffset = 200;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:kShowTime];
[hudManager addGestureInView:view];
}
#pragma mark - 長(zhǎng)時(shí)間的提示語(yǔ)
+ (void) showPermanentMessage:(NSString *)message InView:(UIView *) view{
hudAddedView = view;
[self shareManager];
if (view == nil) {
view = [[UIApplication sharedApplication] windows].lastObject;
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.labelText = message;
hud.removeFromSuperViewOnHide = YES;
hud.mode = MBProgressHUDModeCustomView;
if (hudManager.isShowGloomy) {
//如果添加了view則將botomView的frame修改與view一樣大
if (hudAddedView) {
bottomView.frame = CGRectMake(0, 0, hudAddedView.frame.size.width, hudAddedView.frame.size.height);
}
[view addSubview:bottomView];
[hudManager showBackView];
}
[view bringSubviewToFront:hud];
[hudManager addGestureInView:view];
}
#pragma mark - 網(wǎng)絡(luò)加載提示用
+ (void) showLoadingInView:(UIView *) view{
hudAddedView = view;
[self shareManager];
if (view == nil) {
view = [[UIApplication sharedApplication] windows].lastObject;
}
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
hud.labelText = kLoadingMessage;
hud.removeFromSuperViewOnHide = YES;
if (hudManager.isShowGloomy) {
//如果添加了view則將botomView的frame修改與view一樣大
if (hudAddedView) {
bottomView.frame = CGRectMake(0, 0, hudAddedView.frame.size.width, hudAddedView.frame.size.height);
}
[view addSubview:bottomView];
[hudManager showBackView];
}
[view addSubview:hud];
[hud show:YES];
[hudManager addGestureInView:view];
}
+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title inView:(UIView *)view{
hudAddedView = view;
[self shareManager];
if (view == nil) {
view = [[UIApplication sharedApplication]windows].lastObject;
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
UIImageView *littleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
littleView.image = [UIImage imageNamed:imageName];
hud.customView = littleView;
hud.removeFromSuperViewOnHide = YES;
hud.animationType = MBProgressHUDAnimationZoom;
hud.labelText = title;
hud.mode = MBProgressHUDModeCustomView;
[hud show:YES];
[hud hide:YES afterDelay:kShowTime];
[hudManager addGestureInView:view];
}
#pragma mark - 外部調(diào)用
+(void)showLoading{
[self showLoadingInView:nil];
}
+(void)showBriefAlert:(NSString *)alert{
[self showBriefMessage:alert InView:nil];
}
+(void)showPermanentAlert:(NSString *)alert{
[self showPermanentMessage:alert InView:nil];
}
//+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title{
// [self showAlertWithCustomImage:imageName title:title inView:nil];
//}
#pragma mark - 隱藏提示框
+(void)hideAlert{
[hudManager hideBackView];
UIView *view ;
if (hudAddedView) {
view = hudAddedView;
}else{
view = [[UIApplication sharedApplication].windows lastObject];
}
[self hideHUDForView:view];
}
+ (void)hideHUDForView:(UIView *)view
{
[self hideHUDForView:view animated:YES];
}
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
MBProgressHUD *hud = [self HUDForView:view];
if (hud != nil) {
hud.removeFromSuperViewOnHide = YES;
[hud hide:animated];
return YES;
}
return NO;
}
+ (MBProgressHUD *)HUDForView:(UIView *)view {
NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
for (UIView *subview in subviewsEnum) {
if ([subview isKindOfClass:[MBProgressHUD class]]) {
return (MBProgressHUD *)subview;
}
}
return nil;
}
#pragma mark - 深色背景
-(void)showBackView{
bottomView.hidden = NO;
}
-(void)hideBackView{
bottomView.hidden = YES;
[tap removeTarget:nil action:nil];
bottomView.frame = CGRectMake(0, 0, kScreen_width, kScreen_height);
}
#pragma mark - 添加手勢(shì),觸摸屏幕將提示框隱藏
-(void)addGestureInView:(UIView *)view{
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheScreen)];
tap.delegate = self;
[view addGestureRecognizer:tap];
}
#pragma mark -點(diǎn)擊屏幕
-(void)tapTheScreen{
NSLog(@"點(diǎn)擊屏幕");
[hudManager hideBackView];
[tap removeTarget:nil action:nil];
[MBManager hideAlert];
}
#pragma mark - 解決手勢(shì)沖突
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[MBProgressHUD class]]) {
return YES;
}else{
return NO;
}
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
@end