@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。