利用 MBProgress 提供的接口方法進行了二次封裝拓展.為了更好的使用,特此記錄一下,方便今后開發(fā)使用帶來便捷.
如有更好更優(yōu)的 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 可設(shè)置滯留時間*/
+ (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;
????????// 設(shè)置圖片
????????hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString ?stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
????????// 再設(shè)置模式
????????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 - 設(shè)置展示時間
+ (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
若不想單起一個類也可以在當(dāng)前調(diào)用方法之處手動添加 runloop 來實現(xiàn),具體 code 實現(xiàn)如下:
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];
});
不斷更新中.