MBProgressHUD+Expand - iOS

利用 MBProgress 提供的接口方法進(jìn)行了二次封裝拓展.為了更好的使用,特此記錄一下,方便今后開(kāi)發(fā)使用帶來(lái)便捷.

如有更好更優(yōu)的 demo 歡迎大家分享,共同進(jìn)步!


.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è)置滯留時(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;

????????// 快速顯示一個(gè)提示信息

????????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;

????????// 隱藏時(shí)候從父控件中移除

????????hud.removeFromSuperViewOnHide = YES;

????????// 1秒之后再消失

????????[hud hide:YES afterDelay:0.7];

}

#pragma mark - 顯示錯(cuò)誤信息

+ (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;

????????// 快速顯示一個(gè)提示信息

????????MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];

????????hud.labelText = message;

????????// 隱藏時(shí)候從父控件中移除

????????hud.removeFromSuperViewOnHide = YES;

????????// YES代表需要蒙版效果

????????hud.dimBackground = YES;

????????return hud;

}

#pragma mark - 設(shè)置展示時(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


若不想單起一個(gè)類(lèi)也可以在當(dāng)前調(diào)用方法之處手動(dòng)添加 runloop 來(lái)實(shí)現(xiàn),具體 code 實(shí)現(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:@"請(qǐng)輸入您的賬號(hào)" toView:self.view];

});


不斷更新中.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容