美文网首页
封装MBProgressHUD弹窗

封装MBProgressHUD弹窗

作者: Stone_熊小叔 | 来源:发表于2017-06-14 11:56 被阅读0次

    每个爱啪啪里面都会有提示框,但是自己写太麻烦,还会遇到各种各样的bug。所以呢,封装一下HUD还是很有必要滴~~~,可以直接拿过去用奥,当成项目的工具类就好~!

    part 1.代码部分

    //
    //  QLShowHUD.h
    //  Ueater_v0.1_20170421
    //
    //  Created by 符皓轩 on 2017/6/12.
    //  Copyright © 2017年 符琼林. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    @class QLShowHUD;
    // 定义block
    typedef void (^ConfigShowHUDBlock)(QLShowHUD *config);
    typedef UIView *(^ConfigShowHUDCustomViewBlock)();
    
    // 定义枚举值
    typedef enum {
        Fade    = MBProgressHUDAnimationFade,
        Zoom    = MBProgressHUDAnimationZoom,
        ZoomOut = MBProgressHUDAnimationZoomOut,
        ZoomIn  = MBProgressHUDAnimationZoomIn,
    } HUDAnimationType;
    
    @interface QLShowHUD : NSObject
    @property (nonatomic, assign) HUDAnimationType animationStyle;///动画样式
    @property (nonatomic, strong) NSString *text;/// 文本
    @property (nonatomic, strong) UIFont *textFont;/// 文本字体
    @property (nonatomic, strong) UIView *customView;/// 自定义view  37x37尺寸
    
    // 只显示文本的相关设置
    @property (nonatomic, assign) BOOL showTextOnly;///只显示文本
    @property (nonatomic, assign) float margin;/// 边缘留白
    
    // 颜色设置(设置了颜色之后,透明度就会失效)
    @property (nonatomic, strong) UIColor *backgroundColor;/// 背景颜色
    @property (nonatomic, strong) UIColor *labelColor;/// 文本颜色
    @property (nonatomic, assign) float opacity;/// 透明度
    @property (nonatomic, assign) float cornerRadius;/// 圆角
    
    ///仅仅显示文本并持续几秒的方法
    /* - 使用示例 -
     [ShowHUD showTextOnly:@"请稍后,显示不了..."
     configParameter:^(ShowHUD *config) {
     config.margin          = 10.f;    // 边缘留白
     config.opacity         = 0.7f;    // 设定透明度
     config.cornerRadius    = 2.f;     // 设定圆角
     } duration:3 inView:self.view];
     */
    + (void)showTextOnly:(NSString *)text configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view;
    
    ///显示文本与菊花并持续几秒的方法(文本为nil时只显示菊花)
    /* - 使用示例 -
     [ShowHUD showText:@"请稍后,显示不了..."
     configParameter:^(ShowHUD *config) {
     config.margin          = 10.f;    // 边缘留白
     config.opacity         = 0.7f;    // 设定透明度
     config.cornerRadius    = 2.f;     // 设定圆角
     } duration:3 inView:self.view];
     */
    + (void)showText:(NSString *)text configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view;
    
    ///加载自定义view并持续几秒的方法
    /* - 使用示例 -
     [ShowHUD showText:@"请稍后,显示不了..."
     configParameter:^(ShowHUD *config) {
     config.margin          = 10.f;    // 边缘留白
     config.opacity         = 0.7f;    // 设定透明度
     config.cornerRadius    = 2.f;     // 设定圆角
     } duration:3 inView:self.view];
     */
    + (void)showCustomView:(ConfigShowHUDCustomViewBlock)viewBlock configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view;
    
    + (instancetype)showTextOnly:(NSString *)text configParameter:(ConfigShowHUDBlock)config inView:(UIView *)view;
    
    + (instancetype)showText:(NSString *)text configParameter:(ConfigShowHUDBlock)config inView:(UIView *)view;
    
    + (instancetype)showCustomView:(ConfigShowHUDCustomViewBlock)viewBlock configParameter:(ConfigShowHUDBlock) config inView:(UIView *)view;
    
    - (void)hide;
    @end
    
    代码部分++
    //
    //  QLShowHUD.m
    //  Ueater_v0.1_20170421
    //
    //  Created by 符皓轩 on 2017/6/12.
    //  Copyright © 2017年 符琼林. All rights reserved.
    //
    
    
    
    #ifdef DEBUG
    #define ShowHUD_DLog(fmt, ...) NSLog((@"ShowHUD.m:%s:%d" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
    #else
    #define ShowHUD_DLog(...)
    #endif
    
    #import "QLShowHUD.h"
    
    @interface QLShowHUD ()<MBProgressHUDDelegate>
    {
        MBProgressHUD *_hud;
    }
    @end
    
    @implementation QLShowHUD
    - (instancetype)initWithView:(UIView *)view
    {
        if (view == nil) {
            return nil;
        }
        
        self = [super init];
        if (self) {
            _hud = [[MBProgressHUD alloc] initWithView:view];
            _hud.delegate = self;
            _hud.animationType = MBProgressHUDAnimationZoom; // 默认动画样式
            _hud.removeFromSuperViewOnHide = YES;// 该视图隐藏后则自动从父视图移除掉
            _hud.label.numberOfLines = 0;
            _hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
            [view addSubview:_hud];
        }
        return self;
    }
    
    - (void)hide:(BOOL)hide afterDelay:(NSTimeInterval)delay
    {
        [_hud hideAnimated:hide afterDelay:delay];
    }
    
    - (void)hide
    {
        [_hud hideAnimated:YES];
    }
    
    - (void)show:(BOOL)show
    {
        // 根据属性判断是否要显示文本
        if (_text != nil && _text.length != 0) {
            _hud.label.text = _text;
        }
        
        // 设置文本字体
        if (_textFont) {
            _hud.label.font = _textFont;
        }
        
        // 如果设置这个属性,则只显示文本
        if (_showTextOnly == YES && _text != nil && _text.length != 0) {
            _hud.mode = MBProgressHUDModeText;
        }
        
        // 设置背景色
        if (_backgroundColor) {
            _hud.bezelView.color = _backgroundColor;
        }
        
        // 文本颜色
        if (_labelColor) {
            _hud.label.textColor = _labelColor;
        }
        
        // 设置圆角
        if (_cornerRadius) {
            _hud.bezelView.layer.cornerRadius = _cornerRadius;
        }
        
        // 设置透明度
        if (_opacity) {
            _hud.opacity = _opacity;
        }
        
        // 自定义view
        if (_customView) {
            _hud.mode = MBProgressHUDModeCustomView;
            _hud.customView = _customView;
        }
        
        // 边缘留白
        if (_margin > 0) {
            _hud.margin = _margin;
        }
    
        [_hud showAnimated:show];
    }
    
    #pragma mark - HUD代理方法
    - (void)hudWasHidden:(MBProgressHUD *)hud
    {
        [_hud removeFromSuperview];
        _hud = nil;
    }
    
    #pragma mark - 重写setter方法
    @synthesize animationStyle = _animationStyle;
    
    - (void)setAnimationStyle:(HUDAnimationType)animationStyle
    {
        _animationStyle = animationStyle;
        _hud.animationType = (MBProgressHUDAnimation)_animationStyle;
    }
    
    - (HUDAnimationType)animationStyle
    {
        return _animationStyle;
    }
    
    #pragma mark - 便利的方法
    +(void)showTextOnly:(NSString *)text configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.text = text;
        hud.showTextOnly = YES;
        hud.margin = 10.f;
        
        // 配置额外的参数
        config(hud);
        
        //默认样式
        hud.labelColor = [UIColor whiteColor];
        hud.backgroundColor = [UIColor blackColor];
        hud.cornerRadius = 10;
        // 显示
        [hud show:YES];
        
        // 延迟sec后消失
        [hud hide:YES afterDelay:sec];
    }
    
    +(void)showText:(NSString *)text configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.text = text;
        hud.margin = 10.f;
        
        // 配置额外的参数
        config(hud);
        
        // 显示
        [hud show:YES];
        
        // 延迟sec后消失
        [hud hide:YES afterDelay:sec];
    }
    
    +(void)showCustomView:(ConfigShowHUDCustomViewBlock)viewBlock configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.margin = 10.f;
        
        // 配置额外的参数
        config(hud);
        
        // 自定义View
        hud.customView = viewBlock();
        
        // 显示
        [hud show:YES];
        
        [hud hide:YES afterDelay:sec];
    }
    
    +(instancetype)showTextOnly:(NSString *)text configParameter:(ConfigShowHUDBlock)config inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.text = text;
        hud.showTextOnly = YES;
        hud.margin = 10.f;
        
        // 配置额外的参数
        config(hud);
        
        // 显示
        [hud show:YES];
        
        return hud;
    }
    
    +(instancetype)showText:(NSString *)text configParameter:(ConfigShowHUDBlock)config inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.text = text;
        hud.margin = 10.f;
        
        // 配置额外的参数
        config(hud);
        
        // 显示
        [hud show:YES];
        
        return hud;
    }
    
    + (instancetype)showCustomView:(ConfigShowHUDCustomViewBlock)viewBlock configParameter:(ConfigShowHUDBlock)config inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.margin = 10.f;
        
        // 配置额外的参数
        config(hud);
        
        // 自定义View
        hud.customView = viewBlock();
        
        // 显示
        [hud show:YES];
        
        return hud;
    }
    
    - (void)dealloc
    {
        ShowHUD_DLog(@"资源释放了,没有泄露^_^");
    }
    
    @end
    

    part 2.调用

    [QLShowHUD showTextOnly:@"等小姐姐告诉我该干啥" configParameter:^(JKShowHUD *config) {
            config.cornerRadius = 20;
            config.backgroundColor  = [UIColor blackColor];
            config.labelColor = [UIColor whiteColor];
        } duration:1.0 inView:self.view];
    

    觉得太麻烦的话,可以直接更改内部代码,在内部配置好默认样式,直接调用即可

    [QLShowHUD showTextOnly:@"等小姐姐告诉我该干啥" configParameter:^(JKShowHUD *config) {
        } duration:1.0 inView:self.view];
    

    方法内部修改细节

    +(void)showTextOnly:(NSString *)text configParameter:(ConfigShowHUDBlock)config duration:(NSTimeInterval)sec inView:(UIView *)view
    {
        QLShowHUD *hud = [[QLShowHUD alloc] initWithView:view];
        hud.text = text;
        hud.showTextOnly = YES;
        hud.margin = 10.f;
        config(hud);
        //默认样式
        hud.labelColor = [UIColor whiteColor];
        hud.backgroundColor = [UIColor blackColor];
        hud.cornerRadius = 10;
    
        [hud show:YES];
        [hud hide:YES afterDelay:sec];
    }
    

    part 3.效果图

    提示框.png

    part 4. 懒人传送门

    点我点我QLShowHUD
    还有其他效果就不一一展示啦,有兴趣的小可爱自己玩~觉得不错的给个star...

    相关文章

      网友评论

          本文标题:封装MBProgressHUD弹窗

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