美文网首页
MBManager HUD指示器

MBManager HUD指示器

作者: Lsx_f | 来源:发表于2017-02-15 10:32 被阅读0次

    方便快捷简单的HUD 封装,用一次让你爱不释手....
    版权属于原作者 https://github.com/hungryBoy/alertView

    .h文件
    
    #import <Foundation/Foundation.h>
    #import "MBProgressHUD.h"
    
    static NSString *const kLoadingMessage = @"加载中";
    static CGFloat const   kShowTime  = 1.0f;
    
    @interface MBManager : NSObject
    
    /**
     *  是否显示变淡效果,默认为YES,  PS:只为 showPermanentAlert:(NSString *) alert 和 showLoading 方法添加
     */
    @property (nonatomic, assign) BOOL isShowGloomy;
    /**
     *  显示“加载中”,待圈圈,若要修改直接修改kLoadingMessage的值即可
     */
    + (void) showLoading;
    /**
     *  一直显示自定义提示语,不带圈圈
     *
     *  @param alert 提示信息
     */
    + (void) showPermanentAlert:(NSString *) alert;
    /**
     *  显示简短的提示语,默认2秒钟,时间可直接修改kShowTime
     *
     *  @param alert 提示信息
     */
    + (void) showBriefAlert:(NSString *) alert;
    /**
     *  隐藏alert
     */
    +(void)hideAlert;
    ///**
    // *  自定义加载视图接口,支持自定义图片
    // *
    // *  @param imageName  要显示的图片,最好是37 x 37大小的图片
    // *  @param title 要显示的提示文字
    // */
    //+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title;
    
    /***************************************
     *                                     *
     *  以下方法根据情况可选择使用,一般使用不到  *
     *                                     *
     ***************************************
     */
    
    /**
     *  显示简短提示语到view上
     *
     *  @param message 提示语
     *  @param view    要添加到的view
     */
    + (void) showBriefMessage:(NSString *) message InView:(UIView *) view;
    /**
     *  显示长久的(只要不用手触摸屏幕或者调用hideAlert方法就会一直显示)提示语到view上
     *
     *  @param message 提示语
     *  @param view    要添加到的view
     */
    + (void) showPermanentMessage:(NSString *)message InView:(UIView *) view;
    /**
     *  显示网络加载到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
    
    .m文件
    #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 - 简短提示语
    + (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 - 长时间的提示语
    + (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 - 网络加载提示用
    + (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 - 外部调用
    +(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 - 添加手势,触摸屏幕将提示框隐藏
    -(void)addGestureInView:(UIView *)view{
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheScreen)];
        tap.delegate = self;
        [view addGestureRecognizer:tap];
            
    }
    #pragma mark -点击屏幕
    -(void)tapTheScreen{
        NSLog(@"点击屏幕");
        [hudManager hideBackView];
        [tap removeTarget:nil action:nil];
        [MBManager hideAlert];
    }
    #pragma mark - 解决手势冲突
    -(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
    

    相关文章

      网友评论

          本文标题:MBManager HUD指示器

          本文链接:https://www.haomeiwen.com/subject/vhxqwttx.html