- 常用样式C语法便捷封装
- 定制化使用
- 踩过的坑
⚠️文章中版本为:pod 'MBProgressHUD','1.1.0';后续版本activityIndicatorColor属性被弃用,请自行修改⚠️
一、常用样式C语法便捷封装
话不多说 先上代码
- 根据我当前的项目需求,大致需要三种样式的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
二、定制化使用
1、有一期需求中,需要细微调整样式且不要模糊背景层;发现无论怎么调都会有一个UIVisualEffectView类型的backgroundView衬底,然后缺无法访问控制其隐藏。多次尝试无果,就耍个流氓吧,将其移除😜。
- (void)removeEffectView{
[self.bezelView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.class isEqual:[UIVisualEffectView class]]) {
[obj removeFromSuperview];
}
}];
}
+ (MBProgressHUD *)showTipsStyleHUDWith:(BOOL)canEnabled withTitleFont:(UIFont *)tFont withTitleColor:(UIColor *)titleColor withBezelBGColor:(UIColor *)bezelBGC 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.backgroundView.alpha = 0.85;
tipsStr = tipsStr.length > 0 ? tipsStr : @"😰网络好像有点问题😰";
hud.label.text = tipsStr;
hud.detailsLabel.text = nil;
hud.minSize = CGSizeMake(110, 50);
hud.margin = 20.;
hud.bezelView.layer.cornerRadius = 5.;
hud.label.font = tFont ? tFont : TEXT_FONT(16);
hud.label.textColor = titleColor ? titleColor : [UIColor whiteColor];
hud.label.numberOfLines = 0;
hud.offset = CGPointZero;// CGPointMake(0, -100);
hud.userInteractionEnabled = !canEnabled;
hud.bezelView.backgroundColor = bezelBGC ? bezelBGC : HexColorRGBA(0x000000, 0.75);
[hud hideAnimated:YES afterDelay:MAX(aTime, 1.2) + (tipsStr.length > 25 ? 1 : 0)];
return hud;
}
关于使用
[[MBProgressHUD showTipsStyleHUDWith:YES withTitleFont:TEXT_FONT_Medium(13) withTitleColor:HexColorRGBA(0xffffff, 1) withBezelBGColor:[UIColor clearColor] withTipsContent:@"tips tips tips..." withSuperView:self.containerView withShowTime:2] removeEffectView];
2、有朋友说想等HUD显示结束之后做操作;稍稍把上边函数修改一下给个返回值实现completeBlock即可。如下:
/** 显示自动消失的提示框到 superView aTime 秒
* 会返回该对象本身,如需等待HUD显示结束执行操作可自行实现其completeblock
*/
extern MBProgressHUD * HUDShowTipsToViewATimeAndReturnThis(BOOL canEnabled, NSString *msg, UIView *superView, CGFloat aTime);
MBProgressHUD * HUDShowTipsToViewATimeAndReturnThis(BOOL canEnabled, NSString *msg, UIView *superView, CGFloat aTime){
__block MBProgressHUD *hud;
if (![NSThread isMainThread]) {
@weakObj(superView)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongObj(superView)
hud = [MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:strongSelf withShowTime:MAX(1, aTime) + 0.5];
});
}else hud = [MBProgressHUD showTipsStyleHUDWith:canEnabled withTipsContent:msg withSuperView:superView withShowTime:MAX(1, aTime)];
return hud;
}
[HUDShowTipsToViewATimeAndReturnThis(YES, @"提示一下", nil, 3.) setCompletionBlock:^{
// do some operation
}];
三、踩过的坑😄
为什么说还踩了坑呢?
简单来说就是:子线程切换到主线程更新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。
如果还有一些使用场景没提到的,欢迎评论区留意🤝