基于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

好腦子不如爛筆頭。。。

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

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

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