利用 MBProgress 提供的接口方法进行了二次封装拓展.为了更好的使用,特此记录一下,方便今后开发使用带来便捷.
如有更好更优的 demo 欢迎大家分享,共同进步!
.h
#import "MBProgressHUD.h"
@interface MBProgressHUD (Add)
+ (void)showError:(NSString *)error toView:(UIView *)view;
+ (void)showSuccess:(NSString *)success toView:(UIView *)view;
+ (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view;
/** Hud 可设置滞留时间*/
+ (void)showCommonHudWithAlertString:(NSString *)aString afterDelay:(int)time toView:(UIView *)view;
@end
.m
#import "MBProgressHUD+Expand.h"
@implementation MBProgressHUD (Add)
#pragma mark - 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
if (view == nil) view = [UIApplication sharedApplication].keyWindow;
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.labelText = text;
// 设置图片
hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
// 再设置模式
hud.mode = MBProgressHUDModeCustomView;
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// 1秒之后再消失
[hud hide:YES afterDelay:0.7];
}
#pragma mark - 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view {
[self show:error icon:@"error.png" view:view];
}
+ (void)showError:(NSString *)error afterDelay:(int)time toView:(UIView *)view callback:(void (^)(BOOL, NSError *))callback {
[self showCommonHudWithAlertString:error afterDelay:time toView:view];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(handleHideTimer:) userInfo:nil repeats:NO];//[NSTimer timerWithTimeInterval:time target:self selector:@selector(handleHideTimer:) userInfo:@(YES) repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
// [self show:error icon:@"error.png" view:view];
}
- (void)handleHideTimer:(NSTimer *)timer {
[self hideAnimated:[timer.userInfo boolValue]];
}
+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
[self show:success icon:@"success.png" view:view];
}
#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view {
if (view == nil) view = [UIApplication sharedApplication].keyWindow;
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.labelText = message;
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// YES代表需要蒙版效果
hud.dimBackground = YES;
return hud;
}
#pragma mark - 设置展示时间
+ (void)showCommonHudWithAlertString:(NSString *)aString afterDelay:(int)time toView:(UIView *)view {
MBProgressHUD *commonHud;
if (view == nil) view = [UIApplication sharedApplication].keyWindow;
if (commonHud) {
[commonHud hideAnimated:YES];
commonHud = nil;
}
commonHud = [[MBProgressHUD alloc] initWithView:view];
commonHud.label.text = aString;
commonHud.mode = MBProgressHUDModeText;
commonHud.removeFromSuperViewOnHide = YES;
[view addSubview:commonHud];
[view bringSubviewToFront:commonHud];
[commonHud showAnimated:YES];
[commonHud hideAnimated:YES afterDelay:time];
}
@end
若不想单起一个类也可以在当前调用方法之处手动添加 runloop 来实现,具体 code 实现如下:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// insert your task code here
[MBProgressHUD showError:@"请输入您的账号" toView:self.view];
});
不断更新中.