簡(jiǎn)介
何為 HUD ?即透明指示層,如下圖中間部分:
HUD
HUD 在 iOS 中的界面展示中用的比較多。iOS 開(kāi)發(fā)中最常用的 HUD 要數(shù) MBProgressHUD 和 SVProgressHUD。很多開(kāi)發(fā)者把 HUD 封裝的比較復(fù)雜,本著簡(jiǎn)單、好用的原則,于是對(duì) MBProgressHUD 進(jìn)行了簡(jiǎn)易封裝。地址:https://github.com/Korune/KOProgressHUDManager,后面也會(huì)不定期更新。
思路
- 每個(gè)
+ show...方法,最后調(diào)用的是+ showText: icon: mode: onView: duration:方法。在上面說(shuō)的第二個(gè)方法中,進(jìn)行MBProgressHUD的初始化、相關(guān)設(shè)置。 - 封裝
MBProgressHUD,沒(méi)有通過(guò)添加MBProgressHUD分類(lèi)來(lái)實(shí)現(xiàn),而是通過(guò)KOProgressHUDManager繼承NSObject來(lái)實(shí)現(xiàn),在MBProgressHUD的基礎(chǔ)上多封裝了一層KOProgressHUDManager。 是避免以后改為使用其他第三方HUD(比如SVProgressHUD等)時(shí),將項(xiàng)目中凡是用到MBProgressHUD類(lèi)的很多地方的代碼,都改為第三方HUD類(lèi)。
關(guān)鍵代碼
+ (void)showText:(NSString *)text icon:(UIImage *)icon mode:(MBProgressHUDMode)mode onView:(UIView *)view duration:(CGFloat)duration
{
if (view == nil) {
view = [UIApplication sharedApplication].delegate.window;
}
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.detailsLabel.text = text;
if (mode != MBProgressHUDModeText) {
hud.mode = mode;
} else {
if (icon == nil) {
hud.mode = MBProgressHUDModeText;
} else {
hud.mode = MBProgressHUDModeCustomView;
hud.customView = [[UIImageView alloc] initWithImage:icon];
}
}
hud.animationType = MBProgressHUDAnimationZoom;
hud.removeFromSuperViewOnHide = YES;
if (duration != kKOFastFutureDuration) {
[hud hideAnimated:YES afterDelay:duration];
}
hud.margin = 10;
[self setupHUDAppearance:hud];
}
+ (void)setupHUDAppearance:(MBProgressHUD *)hud
{
hud.detailsLabel.font = [UIFont systemFontOfSize:15];
// hud.detailsLabel.textColor = [UIColor whiteColor];
// hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
// hud.bezelView.backgroundColor = [UIColor brownColor];
}
KOProgressHUDManager 的使用
#pragma mark -
- (IBAction)showSuccess:(id)sender {
[KOProgressHUDManager showSuccess:@"小伙子,你成功了!"];
}
- (IBAction)showError:(id)sender {
[KOProgressHUDManager showError:@"小伙子,暫時(shí)失敗了,不要灰心!" duration:2];
}
- (IBAction)showLoading:(id)sender {
[KOProgressHUDManager showLoading:@"小伙子,等等……" onView:self.view];
}
- (IBAction)hideHUD:(id)sender {
[KOProgressHUDManager hideHUDForView:self.view];
}
截圖

運(yùn)行截圖