美文网首页小知识点好东西
MBProgressHUD的封装(二)

MBProgressHUD的封装(二)

作者: 晓飞90 | 来源:发表于2016-11-24 17:05 被阅读87次

    说明:这篇文章主要是自己开发的时候查阅使用,参考了许多的资料。

    参考源码:https://github.com/TangledHusky/YJProgressHUD

    
    
    #import <Foundation/Foundation.h>
    #import <MBProgressHUD/MBProgressHUD.h>
    
    typedef enum{
        YJProgressModeOnlyText,           //文字
        YJProgressModeLoading,               //加载菊花
        YJProgressModeCircleLoading,         //加载圆形
        YJProgressModeCustomAnimation,       //自定义加载动画(序列帧实现)
        YJProgressModeSuccess                //成功
    }YJProgressMode;
    
    @interface YJProgressHUD : NSObject
    
    //属性
    @property (nonatomic,strong) MBProgressHUD  *hud;
    
    // 方法
    +(instancetype)shareinstance;
    
    //显示
    +(void)show:(NSString *)msg inView:(UIView *)view mode:(YJProgressMode)myMode;
    
    //隐藏
    +(void)hide;
    
    //显示提示(1秒后消失)
    +(void)showMessage:(NSString *)msg inView:(UIView *)view;
    
    //显示提示(N秒后消失)
    +(void)showMessage:(NSString *)msg inView:(UIView *)view afterDelayTime:(NSInteger)delay;
    
    //显示进度(转圈)
    +(MBProgressHUD *)showProgressCircle:(NSString *)msg inView:(UIView *)view;
    
    //显示进度(菊花)
    +(void)showProgress:(NSString *)msg inView:(UIView *)view;
    
    //显示成功提示
    +(void)showSuccess:(NSString *)msg inview:(UIView *)view;
    
    //在最上层显示
    +(void)showMsgWithoutView:(NSString *)msg;
    
    //显示自定义动画(自定义动画序列帧  找UI做就可以了)
    +(void)showCustomAnimation:(NSString *)msg withImgArry:(NSArray *)imgArry inview:(UIView *)view;
    
    @end
    

    方法的实现:主要是学习自定义动画效果

    
    #import "YJProgressHUD.h"
    
    @implementation YJProgressHUD
    
    +(instancetype)shareinstance{
    
        static YJProgressHUD *instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[YJProgressHUD alloc] init];
        });
    
        return instance;
    
    }
    
    +(void)show:(NSString *)msg inView:(UIView *)view mode:(YJProgressMode)myMode{
        [self show:msg inView:view mode:myMode customImgView:nil];
    }
    
    +(void)show:(NSString *)msg inView:(UIView *)view mode:(YJProgressMode)myMode customImgView:(UIImageView *)customImgView{
        //如果已有弹框,先消失
        if ([YJProgressHUD shareinstance].hud != nil) {
            [[YJProgressHUD shareinstance].hud hideAnimated:YES];
            [YJProgressHUD shareinstance].hud = nil;
        }
    
        //4\4s屏幕避免键盘存在时遮挡
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            [view endEditing:YES];
        }
    
        [YJProgressHUD shareinstance].hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        //[YJProgressHUD shareinstance].hud.dimBackground = YES;    //是否显示透明背景
    
        //是否设置黑色背景,这两句配合使用
    //    [YJProgressHUD shareinstance].hud.color
    //    [YJProgressHUD shareinstance].hud.color = [UIColor blackColor];
        [YJProgressHUD shareinstance].hud.contentColor = [UIColor whiteColor];
    
        [[YJProgressHUD shareinstance].hud setMargin:10];
        [[YJProgressHUD shareinstance].hud setRemoveFromSuperViewOnHide:YES];
        [YJProgressHUD shareinstance].hud.detailsLabel.text = msg;
    
        [YJProgressHUD shareinstance].hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        switch ((NSInteger)myMode) {
            case YJProgressModeOnlyText:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeText;
                break;
    
            case YJProgressModeLoading:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeIndeterminate;
                break;
    
    //        case YJProgressModeCircleLoading:{
    //            [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeDeterminate;
    //
    //            break;
    //        }
            case YJProgressModeSuccess:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeCustomView;
                [YJProgressHUD shareinstance].hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success"]];
                break;
            case YJProgressModeCustomAnimation:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeCustomView;
                [YJProgressHUD shareinstance].hud.customView = customImgView;
                break;
            default:
                break;
        }
    }
    
    +(void)hide{
        if ([YJProgressHUD shareinstance].hud != nil) {
            [[YJProgressHUD shareinstance].hud hideAnimated:YES];
        }
    }
    
    +(void)showMessage:(NSString *)msg inView:(UIView *)view{
        [self show:msg inView:view mode:YJProgressModeOnlyText];
        [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    }
    
    +(void)showMessage:(NSString *)msg inView:(UIView *)view afterDelayTime:(NSInteger)delay{
        [self show:msg inView:view mode:YJProgressModeOnlyText];
        [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:delay];
    }
    
    +(void)showSuccess:(NSString *)msg inview:(UIView *)view{
        [self show:msg inView:view mode:YJProgressModeSuccess];
        [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    
    }
    
    +(void)showProgress:(NSString *)msg inView:(UIView *)view{
        [self show:msg inView:view mode:YJProgressModeLoading];
    }
    
    +(MBProgressHUD *)showProgressCircle:(NSString *)msg inView:(UIView *)view{
        if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.mode = MBProgressHUDModeAnnularDeterminate;
        hud.label.text = msg;
        return hud;
    }
    
    +(void)showMsgWithoutView:(NSString *)msg{
        UIWindow *view = [[UIApplication sharedApplication].windows lastObject];
        [self show:msg inView:view mode:YJProgressModeOnlyText];
        [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    
    }
    
    //自定义动画效果
    +(void)showCustomAnimation:(NSString *)msg withImgArry:(NSArray *)imgArry inview:(UIView *)view{
    
        UIImageView *showImageView = [[UIImageView alloc] init];
    
        showImageView.animationImages = imgArry;
        [showImageView setAnimationRepeatCount:0];
        [showImageView setAnimationDuration:(imgArry.count + 1) * 0.75];
        [showImageView startAnimating];
    
        [self show:msg inView:view mode:YJProgressModeCustomAnimation customImgView:showImageView];
    
        //这句话是为了展示几秒,实际要去掉
        [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:8.0];
    }
    @end
    
    

    相关文章

      网友评论

        本文标题:MBProgressHUD的封装(二)

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