项目中需要导入MBProgressHUD和SDWebImage,实现效果如下:

.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class MBProgressHUD;
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger,GLProgressModel) {
GLProgressModelOnlyText, //文字
GLProgressModelLoading, //加载菊花
GLProgressModelCustomImage, //自定义图片
GLProgressModelCustomGif //自定义gif图片
};
@interface GLProgressHUD : NSObject
@property (nonatomic,strong) MBProgressHUD * __nullable hud;
+(instancetype)shareInstance;
//显示菊花和文字
+ (void)showMessage:(NSString *)msg inView:(UIView *)view;
//显示图片、文字
+ (void)showMessage:(NSString *)msg imageName:(NSString *)imageName inView:(UIView *)view;
//只显示文字
+(void)showOnlyText:(NSString *)msg inView:(UIView *)view afterDelay:(NSTimeInterval)delay;
//显示gif图片
+ (void)showMessage:(NSString *)msg gifName:(NSString *)gifName inView:(UIView *)view;
//移除hud
+ (void)hide;
@end
NS_ASSUME_NONNULL_END
.m
#import "GLProgressHUD.h"
#import "UIImage+GIF.h" //导入SDWebImage中的UIImage+GIF.h文件
#import <MBProgressHUD/MBProgressHUD.h>
@implementation GLProgressHUD
static GLProgressHUD *_instance = nil;
+(instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
});
return _instance;
}
+(instancetype)shareInstance{
return [[self alloc] init];
}
-(id)copyWithZone:(NSZone *)zone{
return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return _instance;
}
//只显示文字
+ (void)showOnlyText:(NSString *)msg inView:(UIView *)view afterDelay:(NSTimeInterval)delay{
[self show:msg imageName:nil view:view modeType:GLProgressModelOnlyText afterDelay:delay];
}
//显示菊花和文字
+ (void)showMessage:(NSString *)msg inView:(UIView *)view{
[self show:msg imageName:nil view:view modeType:GLProgressModelLoading afterDelay:0];
}
//显示图片和文字
+ (void)showMessage:(NSString *)msg imageName:(NSString *)imageName inView:(UIView *)view{
[self show:msg imageName:imageName view:view modeType:GLProgressModelCustomImage afterDelay:0];
}
+ (void)showMessage:(NSString *)msg gifName:(NSString *)gifName inView:(UIView *)view{
[self show:msg imageName:gifName view:view modeType:GLProgressModelCustomGif afterDelay:0];
}
+ (void)show:(NSString *)text imageName:(NSString *)imageName view:(UIView *)view modeType:(GLProgressModel)modeType afterDelay:(NSTimeInterval)delay{
if (!view) {
view = [[UIApplication sharedApplication].windows lastObject];
}
//如果已有弹框,先消失
if ([GLProgressHUD shareInstance].hud != nil) {
[[GLProgressHUD shareInstance].hud hideAnimated:YES];
[GLProgressHUD shareInstance].hud = nil;
}
[GLProgressHUD shareInstance].hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
[GLProgressHUD shareInstance].hud.detailsLabel.text = text;
switch (modeType) {
case GLProgressModelOnlyText:
[GLProgressHUD shareInstance].hud.mode = MBProgressHUDModeText;
[[GLProgressHUD shareInstance].hud hideAnimated:YES afterDelay:delay];
break;
case GLProgressModelLoading:
[GLProgressHUD shareInstance].hud.mode = MBProgressHUDModeIndeterminate;
break;
case GLProgressModelCustomImage:
[GLProgressHUD shareInstance].hud.mode = MBProgressHUDModeCustomView;
[GLProgressHUD shareInstance].hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
break;
case GLProgressModelCustomGif:
[GLProgressHUD shareInstance].hud.customView = [[UIImageView alloc] initWithImage:[UIImage sd_animatedGIFWithData:[self creatImageDataWithName:imageName]]];
[GLProgressHUD shareInstance].hud.mode = MBProgressHUDModeCustomView;
break;
default:
break;
}
}
+ (void)hide{
[[GLProgressHUD shareInstance].hud hideAnimated:YES];
}
+ (NSData *)creatImageDataWithName:(NSString *)imageName{
return [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"gif"]];
}
网友评论