對第三方插件MBProgressHUD做一個簡單的封裝
github下載地址: https://github.com/jiangbin1993/MBProgressHUD-JJ.git
下載的文件
從github上下載下來文件后局蚀,主要有demo和封裝的文件
下載文件圖.png
封裝的文件代碼展示:
文件MBProgressHUD+JJ.h
/**
github:https://github.com/jiangbin1993/MBProgressHUD-JJ.git
作者:Jonas
**/
#import "MBProgressHUD.h"
// 統(tǒng)一的顯示時長
#define kHudShowTime 1.5
@interface MBProgressHUD (JJ)
#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
文件MBProgressHUD+JJ.m
#import "MBProgressHUD+JJ.h"
@implementation MBProgressHUD (JJ)
#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.labelText = text;
// 判斷是否顯示圖片
if (icon == nil) {
hud.mode = MBProgressHUDModeText;
}else{
// 設(shè)置圖片
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]];
img = img == nil ? [UIImage imageNamed:icon] : img;
hud.customView = [[UIImageView alloc] initWithImage:img];
// 再設(shè)置模式
hud.mode = MBProgressHUDModeCustomView;
}
// 隱藏時候從父控件中移除
hud.removeFromSuperViewOnHide = YES;
// 指定時間之后再消失
[hud hide: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.labelText = message;
// 細(xì)節(jié)文字
// hud.detailsLabelText = @"請耐心等待";
// 再設(shè)置模式
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.labelText = @"加載中...";
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)hideHUDForView:(UIView *)view{
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
[self hideHUDForView:view animated:YES];
}
+ (void)hideHUD{
[self hideHUDForView:nil];
}
@end
如何使用婚度?
將下載下來的工程文件夾內(nèi)的MBProgressHUD文件夾整個導(dǎo)入工程中
在使用的地方 #import "MBProgressHUD+JJ.h"
現(xiàn)在封裝幾個功能
一秋忙、提示普通信息
[MBProgressHUD showMessage:@"提示信息:你被包圍了凯旭!哈哈哈哈Y旌!R闰稹违霞!"];
彈出提示信息gif
二混聊、提示帶圖片的信息
錯誤提示
代碼:
[MBProgressHUD showError:@"出錯了暗!"];
提示錯誤gif
成功提示
[MBProgressHUD showSuccess:@"成功了句喜!"];
提示成功gif
警告提示
[MBProgressHUD showWarning:@"警告注意!"];
警告提示gif
自定義圖片文字提示
[MBProgressHUD showMessageWithImageName:@"MBHUD_Info" message:@"哈哈哈僵闯!"];
自定義圖片提示gif
三、加載中
MBProgressHUD *hud = [MBProgressHUD showActivityMessage:@"加載中..."];
[hud hide:YES afterDelay:1.5]; // 1.5秒后移除
加載中g(shù)if
四藤滥、進(jìn)度條
// 模擬網(wǎng)絡(luò)請求進(jìn)度
dispatch_async(dispatch_get_global_queue(0, 0), ^{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
// 主線程刷新進(jìn)度
dispatch_async(dispatch_get_main_queue(), ^{
hud.progress = progress;
});
// 進(jìn)程掛起50毫秒
usleep(50000);
}
// 100%后移除
dispatch_async(dispatch_get_main_queue(), ^{
[hud hide:YES];
});
});
進(jìn)度條gif
使用詳情查看demo