基于MBProgressHUD的封裝

這里有源碼分析: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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、前言 在ios開(kāi)發(fā)中,最經(jīng)典也是最常用的提示框就是MBProgressHUD了,用于在執(zhí)行一些任務(wù)時(shí)的提示效果...
    MrFire_閱讀 14,040評(píng)論 22 64
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評(píng)論 25 709
  • 文:zesheng liu 一身的冷汗,嚇?biāo)缹殞毩恕?昨天瞌睡來(lái)的很早,迷迷糊糊睡了。 開(kāi)始是來(lái)到了一個(gè)很熱鬧的地...
    liuzesheng閱讀 771評(píng)論 25 20
  • 教官:后天你們要扛槍?zhuān)?斤重 某人:報(bào)告,是真槍嗎 教官:你襠里不是有真槍嗎 報(bào)告,沒(méi)有8斤
    落幕時(shí)祈禱閱讀 200評(píng)論 0 1
  • 文/沉麗 生活像一把無(wú)情刻刀 改變了我們模樣 未曾綻放就要枯...
    沉七閱讀 1,168評(píng)論 11 28

友情鏈接更多精彩內(nèi)容