基于MBProgressHUD封装

#import "MBProgressHUD.h"

NS_ASSUME_NONNULL_BEGIN

typedefNS_ENUM(NSInteger, ZFProgressHUDStatus) {

    /** 成功 */

    ZFProgressHUDStatusSuccess,

    /** 失败 */

    ZFProgressHUDStatusError,

    /** 警告*/

    ZFProgressHUDStatusWaitting,

    /** 提示 */

    ZFProgressHUDStatusInfo,

    /** 等待 */

    ZFProgressHUDStatusLoading

};

@interfaceZFProgressHUD :MBProgressHUD

/**

 *  是否正在显示

 */

@property (nonatomic, assign, getter=isShowNow) BOOL showNow;

/** 返回一个 HUD 的单例 */

+ (instancetype)sharedHUD;

/** 在 window 上添加一个 HUD */

+ (void)showStatus:(ZFProgressHUDStatus)status text:(NSString*)text;

#pragma mark - 建议使用的方法

/** 在 window 上添加一个只显示文字的 HUD */

+ (void)showMessage:(NSString*)text;

/** 在 window 上添加一个提示`信息`的 HUD */

+ (void)showWaiting:(NSString*)text;

/** 在 window 上添加一个提示`失败`的 HUD */

+ (void)showError:(NSString*)text;

/** 在 window 上添加一个提示`成功`的 HUD */

+ (void)showSuccess:(NSString*)text;

/** 在 window 上添加一个提示`等待`的 HUD, 需要手动关闭 */

+ (void)showLoading:(NSString*)text;

/** 手动隐藏 HUD */

+ (void)hideHUD;

@end

NS_ASSUME_NONNULL_END



#import "ZFProgressHUD.h"

// 背景视图的宽度/高度

#define BGVIEW_WIDTH100.0f

// 文字大小

#define TEXT_SIZE    16.0f

@implementation ZFProgressHUD

+ (instancetype)sharedHUD {

    staticidhud;

    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        hud = [[self alloc] initWithView:[UIApplication sharedApplication].keyWindow];

    });

    returnhud;

}

+ (void)showStatus:(ZFProgressHUDStatus)status text:(NSString*)text {

    ZFProgressHUD *HUD = [ZFProgressHUD sharedHUD];

    HUD.bezelView.color = [UIColor blackColor];

    HUD.contentColor=[UIColor whiteColor];

    [HUDshowAnimated:YES];

    [HUDsetShowNow:YES];

    //蒙版显示 YES , NO 不显示

    //        HUD.dimBackground = YES;

    HUD.label.text= text;

    HUD.label.textColor = [UIColor whiteColor];

    [HUDsetRemoveFromSuperViewOnHide:YES];

    HUD.label.font = [UIFont boldSystemFontOfSize:TEXT_SIZE];

    [HUDsetMinSize:CGSizeMake(BGVIEW_WIDTH, BGVIEW_WIDTH)];

    [[UIApplication sharedApplication].keyWindow addSubview:HUD];

    //图片文件获取

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"ZFProgressHUD" ofType:@"bundle"];

    switch(status) {

        case ZFProgressHUDStatusSuccess: {

            NSString*sucPath = [bundlePathstringByAppendingPathComponent:@"MBHUD_Success.png"];

            UIImage *sucImage = [UIImage imageWithContentsOfFile:sucPath];

            HUD.mode = MBProgressHUDModeCustomView;

            UIImageView *sucView = [[UIImageView alloc] initWithImage:sucImage];

            HUD.customView= sucView;

            [HUDhideAnimated:YES afterDelay:2.0f];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [HUDsetShowNow:NO];

            });

        }

            break;

        case ZFProgressHUDStatusError: {

            NSString*errPath = [bundlePathstringByAppendingPathComponent:@"MBHUD_Error.png"];

            UIImage *errImage = [UIImage imageWithContentsOfFile:errPath];

            HUD.mode = MBProgressHUDModeCustomView;

            UIImageView *errView = [[UIImageView alloc] initWithImage:errImage];

            HUD.customView= errView;

            [HUDhideAnimated:YES afterDelay:2.0f];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [HUDsetShowNow:NO];

            });

        }

            break;

        case ZFProgressHUDStatusLoading: {

            HUD.mode = MBProgressHUDModeIndeterminate;

        }

            break;

        case ZFProgressHUDStatusWaitting: {

            NSString*infoPath = [bundlePathstringByAppendingPathComponent:@"MBHUD_Warn.png"];

            UIImage*infoImage = [UIImageimageWithContentsOfFile:infoPath];

            HUD.mode = MBProgressHUDModeCustomView;

            UIImageView*infoView = [[UIImageViewalloc]initWithImage:infoImage];

            HUD.customView= infoView;

            [HUDhideAnimated:YES afterDelay:2.0f];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [HUDsetShowNow:NO];

            });

        }

            break;

        case ZFProgressHUDStatusInfo: {

            NSString*infoPath = [bundlePathstringByAppendingPathComponent:@"MBHUD_Info.png"];

            UIImage*infoImage = [UIImageimageWithContentsOfFile:infoPath];

            HUD.mode = MBProgressHUDModeCustomView;

            UIImageView*infoView = [[UIImageViewalloc]initWithImage:infoImage];

            HUD.customView= infoView;

            [HUDhideAnimated:YES afterDelay:2.0f];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [HUDsetShowNow:NO];

            });

        }

            break;

        default:

            break;

    }

}

+ (void)showMessage:(NSString*)text {

    ZFProgressHUD *HUD = [ZFProgressHUD sharedHUD];

    HUD.bezelView.color = [UIColor blackColor];

    [HUDshowAnimated:YES];

    [HUDsetShowNow:YES];

    HUD.label.text= text;

    HUD.contentColor=[UIColor whiteColor];

    [HUDsetMinSize:CGSizeZero];

    [HUDsetMode:MBProgressHUDModeText];

    //    HUD.dimBackground = YES;

    [HUDsetRemoveFromSuperViewOnHide:YES];

    HUD.label.font = [UIFont boldSystemFontOfSize:TEXT_SIZE];

    [[UIApplication sharedApplication].keyWindow addSubview:HUD];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [[ZFProgressHUD sharedHUD] setShowNow:NO];

        [[ZFProgressHUD sharedHUD] hideAnimated:YES];

    });

}

+ (void)showWaiting:(NSString*)text {

    [self showStatus:ZFProgressHUDStatusWaitting text:text];

}

+ (void)showError:(NSString*)text {

    [self showStatus:ZFProgressHUDStatusError text:text];

}

+ (void)showSuccess:(NSString*)text {

    [self showStatus:ZFProgressHUDStatusSuccess text:text];

}

+ (void)showLoading:(NSString*)text {

    [self showStatus:ZFProgressHUDStatusLoading text:text];

}

+ (void)hideHUD {

    [[ZFProgressHUD sharedHUD] setShowNow:NO];

    [[ZFProgressHUD sharedHUD] hideAnimated:YES];

}

@end

好脑子不如烂笔头。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容