结合了比较火的三方库MBProgressHUD
, 但是MBProgressHUD
用起来代码比较繁琐, 所以搞了一个Category
方便在项目里使用
.h文件
#import "MBProgressHUD.h"
// 统一的显示时长
#define kHudShowTime 1.5
@interface MBProgressHUD (MaxMethod)
#pragma mark 在指定的view上显示hud
+ (void)showMessage:(NSString *)message toView:(UIView *)view;
+ (void)showSuccess:(NSString *)success toView:(UIView *)view;
+ (void)showError:(NSString *)error toView:(UIView *)view;
+ (void)showWarning:(NSString *)Warning toView:(UIView *)view;
+ (void)showMessageWithImageName:(NSString *)imageName message:(NSString *)message toView:(UIView *)view;
+ (MBProgressHUD *)showActivityMessage:(NSString*)message view:(UIView *)view;
+ (MBProgressHUD *)showProgressBarToView:(UIView *)view;
#pragma mark 在window上显示hud
+ (void)showMessage:(NSString *)message;
+ (void)showSuccess:(NSString *)success;
+ (void)showError:(NSString *)error;
+ (void)showWarning:(NSString *)Warning;
+ (void)showMessageWithImageName:(NSString *)imageName message:(NSString *)message;
+ (MBProgressHUD *)showActivityMessage:(NSString*)message;
#pragma mark 移除hud
+ (void)hideHUDForView:(UIView *)view;
+ (void)hideHUD;
@end
.m文件
#import "MBProgressHUD+MaxMethod.h"
@implementation MBProgressHUD (MaxMethod)
#pragma mark 显示一条信息
+ (void)showMessage:(NSString *)message toView:(UIView *)view {
[self show:message icon:nil view:view];
}
#pragma mark 显示带图片或者不带图片的信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.label.text = text;
// 判断是否显示图片
if (icon == nil) {
hud.mode = MBProgressHUDModeText;
}else{
// 设置图片
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]];
img = img == nil ? [UIImage imageNamed:icon] : img;
hud.customView = [[UIImageView alloc] initWithImage:img];
// 再设置模式
hud.mode = MBProgressHUDModeCustomView;
}
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// 指定时间之后再消失
// [hud hide:YES afterDelay:kHudShowTime];
[hud hideAnimated:YES afterDelay:kHudShowTime];
}
#pragma mark 显示成功信息
+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
[self show:success icon:@"success.png" view:view];
}
#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view {
[self show:error icon:@"error.png" view:view];
}
#pragma mark 显示警告信息
+ (void)showWarning:(NSString *)Warning toView:(UIView *)view {
[self show:Warning icon:@"warn" view:view];
}
#pragma mark 显示自定义图片信息
+ (void)showMessageWithImageName:(NSString *)imageName message:(NSString *)message toView:(UIView *)view {
[self show:message icon:imageName view:view];
}
#pragma mark 加载中
+ (MBProgressHUD *)showActivityMessage:(NSString*)message view:(UIView *)view {
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.label.text = message;
// 细节文字
// hud.detailsLabelText = @"请耐心等待";
// 再设置模式
hud.mode = MBProgressHUDModeIndeterminate;
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
return hud;
}
+ (MBProgressHUD *)showProgressBarToView:(UIView *)view {
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
hud.label.text = @"Loading...";
return hud;
}
+ (void)showMessage:(NSString *)message {
[self showMessage:message toView:nil];
}
+ (void)showSuccess:(NSString *)success {
[self showSuccess:success toView:nil];
}
+ (void)showError:(NSString *)error {
[self showError:error toView:nil];
}
+ (void)showWarning:(NSString *)Warning {
[self showWarning:Warning toView:nil];
}
+ (void)showMessageWithImageName:(NSString *)imageName message:(NSString *)message {
[self showMessageWithImageName:imageName message:message toView:nil];
}
+ (MBProgressHUD *)showActivityMessage:(NSString*)message {
return [self showActivityMessage:message view:nil];
}
//+ (void)showProgress:(float)progress status:(NSString *)status {
// UIView *view = [[UIApplication sharedApplication].windows lastObject];
// MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
// hud.mode = MBProgressHUDModeDeterminate;
// hud.label.text = status;
//
// dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
//
// dispatch_async(dispatch_get_main_queue(), ^{
// [hud hideAnimated:YES];
// });
// });
//}
+ (void)hideHUDForView:(UIView *)view {
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
[self hideHUDForView:view animated:YES];
}
+ (void)hideHUD {
[self hideHUDForView:nil];
}
@end
网友评论