話不多說 先上代碼
- 根據我目前的項目需求,大緻需要三種樣式的HUD:菊花轉、提示框、上傳下載下傳進度展示。為了友善使用,抽成了C方法,頭檔案部分如下。
@interface MBProgressHUD (YTTool)
/** 顯示僅有菊花的loading框 到 window HUD是否需要霸屏 */
extern void HUDShowOnlyLoading(BOOL canEnabled);
/** 顯示僅有菊花的loading框 到 superView HUD是否需要霸屏 */
extern void HUDShowOnlyLoadingToView(BOOL canEnabled, UIView *superView);
/** 顯示僅有菊花的loading框 到 superView atime 秒 HUD是否需要霸屏 */
extern void HUDShowOnlyLoadingToViewATime(BOOL canEnabled, UIView *superView, CGFloat atime);
/** 顯示自動消失的提示框到 window */
extern void HUDShowTips(NSString *msg,BOOL canEnabled);
/** 顯示自動消失的提示框到 superView 2 秒 */
extern void HUDShowTipsToView(BOOL canEnabled, NSString *msg,UIView *superView);
/** 顯示自動消失的提示框到 superView aTime 秒 */
extern void HUDShowTipsToViewATime(BOOL canEnabled, NSString *msg, UIView *superView, CGFloat aTime);
/** 顯示上傳或下載下傳圓環進度框 */
extern void HUDShowDownLoadProgress(CGFloat progress);
extern void HUDShowProgress(NSString *msg, CGFloat progress);
extern void HUDShowProgressToView(NSString *msg, CGFloat progress, UIView *superView);
/** 隐藏所有HUD */
extern void HideHUD(UIView *view);
@end
- 具體實作如下:
@implementation MBProgressHUD (FYTool)
#pragma mark - loading
/** 顯示僅有菊花的loading框 到 window HUD是否需要霸屏 */
void HUDShowOnlyLoading(BOOL canEnabled) {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showLoadingStyleHUDWith:canEnabled withSuperView:KEYWINDOW withShowTime:0];
});
}else [MBProgressHUD showLoadingStyleHUDWith:canEnabled withSuperView:KEYWINDOW withShowTime:0];
}
/** 顯示僅有菊花的loading框 到 superView HUD是否需要霸屏 */
void HUDShowOnlyLoadingToView(BOOL canEnabled, UIView *superView) {
if (![NSThread isMainThread]) {
@weakObj(superView)
dispatch_async(dispatch_get_main_queue(), ^{
@strongObj(superView)
[MBProgressHUD showLoadingStyleHUDWith:canEnabled withSuperView:strongSelf withShowTime:0];
});
}else [MBProgressHUD showLoadingStyleHUDWith:canEnabled withSuperView:superView withShowTime:0];
}
/** 顯示僅有菊花的loading框 到 superView atime 秒 HUD是否需要霸屏 */
void HUDShowOnlyLoadingToViewATime(BOOL canEnabled, UIView *superView, CGFloat atime){
if (![NSThread isMainThread]) {
@weakObj(superView)
dispatch_async(dispatch_get_main_queue(), ^{
@strongObj(superView)
[MBProgressHUD showLoadingStyleHUDWith:canEnabled withSuperView:strongSelf withShowTime:atime];
});
}else [MBProgressHUD showLoadingStyleHUDWith:canEnabled withSuperView:superView withShowTime:atime];
}
#pragma mark - tips
/** 顯示自動消失的提示框到 window */
void HUDShowTips(NSString *msg,BOOL canEnabled){
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:KEYWINDOW withShowTime:0];
});
}else [MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:KEYWINDOW withShowTime:0];
}
/** 顯示自動消失的提示框到 superView 2 秒 */
void HUDShowTipsToView(BOOL canEnabled, NSString *msg,UIView *superView) {
if (![NSThread isMainThread]) {
@weakObj(superView)
dispatch_async(dispatch_get_main_queue(), ^{
@strongObj(superView)
[MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:strongSelf withShowTime:0];
});
}else [MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:superView withShowTime:0];
}
/** 顯示自動消失的提示框到 superView aTime 秒 */
void HUDShowTipsToViewATime(BOOL canEnabled, NSString *msg, UIView *superView, CGFloat aTime) {
if (![NSThread isMainThread]) {
@weakObj(superView)
dispatch_async(dispatch_get_main_queue(), ^{
@strongObj(superView)
[MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:strongSelf withShowTime:aTime];
});
}else [MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:superView withShowTime:aTime];
}
#pragma mark - progress
void HUDShowDownLoadProgress(CGFloat progress) {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showProgressHUDWith:nil withSuperView:KEYWINDOW withProgress:progress];
});
}else
[MBProgressHUD showProgressHUDWith:nil withSuperView:KEYWINDOW withProgress:progress];
}
void HUDShowProgress(NSString *msg, CGFloat progress) {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showProgressHUDWith:msg withSuperView:KEYWINDOW withProgress:progress];
});
}else [MBProgressHUD showProgressHUDWith:msg withSuperView:KEYWINDOW withProgress:progress];
}
void HUDShowProgressToView(NSString *msg, CGFloat progress, UIView *superView){
if (![NSThread isMainThread]) {
@weakObj(superView)
dispatch_async(dispatch_get_main_queue(), ^{
@strongObj(superView)
[MBProgressHUD showProgressHUDWith:msg withSuperView:strongSelf withProgress:progress];
});
}else [MBProgressHUD showProgressHUDWith:msg withSuperView:superView withProgress:progress];
}
#pragma mark - hide
/** 隐藏所有HUD */
void HideHUD(UIView *view) {
if (![NSThread isMainThread]) {
@weakObj(view)
dispatch_async(dispatch_get_main_queue(), ^{
@strongObj(view)
[MBProgressHUD hideHUDWithSuperViw:strongSelf];
});
}else{
[MBProgressHUD hideHUDWithSuperViw:view];
}
}
#pragma mark -
+ (void)showLoadingStyleHUDWith:(BOOL)canEnabled withSuperView:(UIView *)view withShowTime:(CGFloat)aTime {
MBProgressHUD * hud = [MBProgressHUD HUDForView:view];
if (!hud) {
hud =[MBProgressHUD showHUDAddedTo:view ? view : KEYWINDOW animated:YES];
}else{
[hud showAnimated:YES];
}
hud.minSize = CGSizeMake(60, 60);
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.backgroundColor = HexColorRGBA(0x000000, 0.2);
hud.bezelView.layer.cornerRadius = 10.;
hud.mode = MBProgressHUDModeIndeterminate; //菊花,預設值
hud.label.text = nil;
hud.detailsLabel.text = nil;
hud.activityIndicatorColor = [UIColor whiteColor];
hud.offset = CGPointZero;
// [hud setContentColor:[UIColor whiteColor]];
hud.tintColor = [UIColor whiteColor];
hud.removeFromSuperViewOnHide = YES;
hud.userInteractionEnabled = !canEnabled;
if (aTime > 0.5) {
[hud hideAnimated:YES afterDelay:aTime];
}
}
+ (void)showTipsStyleHUDWith:(BOOL)canEnabled withTipsContent:(NSString *)tipsStr withSuperView:(UIView *)view withShowTime:(CGFloat)aTime {
MBProgressHUD * hud = [MBProgressHUD HUDForView:view];
if (!hud) {
hud =[MBProgressHUD showHUDAddedTo:view ? view : KEYWINDOW animated:YES];
}
hud.mode = MBProgressHUDModeText;
hud.animationType = MBProgressHUDAnimationZoomOut;
hud.bezelView.backgroundColor = HexColorRGBA(0x000000, 0.85);
hud.backgroundView.alpha = 0.85;
hud.label.text = tipsStr.length > 0 ? tipsStr : @"?網絡好像有點問題?";
hud.detailsLabel.text = nil;
hud.minSize = CGSizeMake(110, 40);
hud.margin = 15.;
hud.bezelView.layer.cornerRadius = 20.;
hud.label.font=[UIFont systemFontOfSize:16];
hud.label.textColor = HEXCOLOR(0xf0f0f0); //[UIColor whiteColor];
hud.label.numberOfLines = 0;
hud.offset = CGPointMake(0, -100);
hud.userInteractionEnabled = !canEnabled;
if (aTime > 0.5) {
[hud hideAnimated:YES afterDelay:aTime];
}
}
+ (void)showProgressHUDWith:(NSString *)tipsStr withSuperView:(UIView *)view withProgress:(CGFloat)progress{
MBProgressHUD * hud = [MBProgressHUD HUDForView:view];
if (!hud) {
hud =[MBProgressHUD showHUDAddedTo:view ? view : KEYWINDOW animated:YES];
hud.progress = MAX(0.01, progress);
}else{
hud.progress = MAX(0.01, progress);
[hud showAnimated:YES];
}
hud.minSize = CGSizeMake(160, 80);
hud.label.text = [NSString stringWithFormat:@"%2.f%%",MAX(0.01, progress)*100];
hud.label.font = [UIFont systemFontOfSize:12];
hud.label.textColor = [UIColor whiteColor];
hud.label.numberOfLines = 0;
hud.offset = CGPointMake(0, -40);//相對于hud父視圖偏移
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.backgroundColor = HexColorRGBA(0x000000, 0.25);
hud.detailsLabel.text = tipsStr.length > 0 ? tipsStr :@"正在儲存到本地";
hud.detailsLabel.textColor = [UIColor whiteColor];
hud.mode = MBProgressHUDModeAnnularDeterminate;//圓環作為進度條
hud.removeFromSuperViewOnHide = YES;
[hud setContentColor:[UIColor whiteColor]];//HEXCOLOR(0x448bf2)
hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
if (hud.progress >= 1) {
[hud hideAnimated:YES afterDelay:1];
}
}
#pragma mark -
+ (void)hideHUDWithSuperViw:(UIView *)view{
if (view) {
[MBProgressHUD hideHUDForView:view animated:YES];
}
[MBProgressHUD hideHUDForView:KEYWINDOW animated:YES];
}
@end
-------------------下邊就是坑?----------------------------
為什麼說還踩了坑呢?
簡單來說就是:子線程切換到主線程更新HUD的展示,例如上傳和下載下傳的時候。
主要是項目中接入了 阿裡雲的OSS ,既然支援了用戶端直傳,那下載下傳也必然會用OSS的SDK自帶的?~
用過OSS SDK的應該都知道,OSSTask這個類有個waitUntilFinished方法,使任務串行執行,可以在批量上傳的時候確定順序。然而在使其task等待的同時,這個task的OSSGetObjectRequest對象中的downloadProgress進度回調也會被等待,進而使
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {};
也被等待,使downloadProgress代碼塊内更新HUD的任務被堆積。一直到等待結束瞬間執行完堆積的N次更新。。。
###------------------------------------------------
最後,值得注意的是,之前看有人發現偶爾會有顯示出的hud隐藏不掉,個人感覺大概有兩種可能:
1、hideHUDWithSuperViw方法隐藏的是你指定view上添加的HUD,show的時候和hide的時候傳入的view不是同一個将會隐藏不掉。
2、在viewcontroller裡的某個block外進行了HUD的添加顯示,block内進行hide,此時的self是哪個weak修飾過的,當VC被釋放的時候也有可能隐藏不掉hud。這種情況個人建議先weak再strong。