[TOC]
MBProgressHUD 是iOS 第三方Hub 框控件喇肋。
結(jié)構(gòu)
控件組成
在MBProgressHUD(以下簡稱MB)中hub 的結(jié)構(gòu)是由幾部分組成的
- indicatorView 展示菊花农渊,進(jìn)度條等樣式的視圖雇逞。根據(jù)不同mode來創(chuàng)建不同的View麻裁。
- backgroundView背景View期升,處理暗色拜姿,或者模糊樣式峻汉,對應(yīng)類MBBackgroundView
typedef NS_ENUM(NSInteger, MBProgressHUDBackgroundStyle) {
MBProgressHUDBackgroundStyleSolidColor,
MBProgressHUDBackgroundStyleBlur
};
- label 加載框下方的描述信息
- detailsLabel 描述信息下面的詳細(xì)描述信息
- actionButton 在提供一個操作的button
樣式
在MBProgressHUD(以下簡稱MB)的頭文件中描述了幾種樣式
typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
/// 無限菊花樣式.
MBProgressHUDModeIndeterminate,
/// 圓形進(jìn)度條樣式.
MBProgressHUDModeDeterminate,
/// 橫向滾動進(jìn)度條樣式.
MBProgressHUDModeDeterminateHorizontalBar,
/// 圓形進(jìn)度條樣式.和第二個有點(diǎn)不同
MBProgressHUDModeAnnularDeterminate,
/// 自定義View的樣式
MBProgressHUDModeCustomView,
/// 只有文字的樣式.
MBProgressHUDModeText
};
MB 通過MBProgressHUDMode來創(chuàng)建不同展示Hub
- (void)updateIndicators {
UIView *indicator = self.indicator;
BOOL isActivityIndicator = [indicator isKindOfClass:[UIActivityIndicatorView class]];
BOOL isRoundIndicator = [indicator isKindOfClass:[MBRoundProgressView class]];
MBProgressHUDMode mode = self.mode;
if (mode == MBProgressHUDModeIndeterminate) {
if (!isActivityIndicator) {
// Update to indeterminate indicator
[indicator removeFromSuperview];
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[(UIActivityIndicatorView *)indicator startAnimating];
[self.bezelView addSubview:indicator];
}
}
else if (mode == MBProgressHUDModeDeterminateHorizontalBar) {
// Update to bar determinate indicator
[indicator removeFromSuperview];
indicator = [[MBBarProgressView alloc] init];
[self.bezelView addSubview:indicator];
}
else if (mode == MBProgressHUDModeDeterminate || mode == MBProgressHUDModeAnnularDeterminate) {
if (!isRoundIndicator) {
// Update to determinante indicator
[indicator removeFromSuperview];
indicator = [[MBRoundProgressView alloc] init];
[self.bezelView addSubview:indicator];
}
if (mode == MBProgressHUDModeAnnularDeterminate) {
[(MBRoundProgressView *)indicator setAnnular:YES];
}
}
else if (mode == MBProgressHUDModeCustomView && self.customView != indicator) {
// Update custom view indicator
[indicator removeFromSuperview];
indicator = self.customView;
[self.bezelView addSubview:indicator];
}
else if (mode == MBProgressHUDModeText) {
[indicator removeFromSuperview];
indicator = nil;
}
indicator.translatesAutoresizingMaskIntoConstraints = NO;
self.indicator = indicator;
if ([indicator respondsToSelector:@selector(setProgress:)]) {
[(id)indicator setValue:@(self.progress) forKey:@"progress"];
}
[indicator setContentCompressionResistancePriority:998.f forAxis:UILayoutConstraintAxisHorizontal];
[indicator setContentCompressionResistancePriority:998.f forAxis:UILayoutConstraintAxisVertical];
[self updateViewsForColor:self.contentColor];
[self setNeedsUpdateConstraints];
}
具體實(shí)現(xiàn)
MB 的實(shí)現(xiàn)比較樸實(shí)贴汪,沒有過多花哨的技巧,簡單實(shí)用休吠。
展示的流程大概是這樣的
- 通過
initWithWindow
和initWithView
來創(chuàng)建Hub 扳埂,這里的Window和View不是MB 來添加的父視圖而是算確定frame的視圖 - 通過
showHUDAddedTo
來將Hub 添加到指定的父視圖。 - 調(diào)用
showAnimated
通過指定動畫來展示出Hub - 實(shí)用
hideAnimated
來指定動畫消失
其他
MB 通過通知UIApplicationDidChangeStatusBarOrientationNotification
來處理屏幕轉(zhuǎn)屏事件
- (void)registerForNotifications {
#if !TARGET_OS_TV
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(statusBarOrientationDidChange:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
#endif
}
MB 的MBProgressHUDModeDeterminateHorizontalBar模式是通過drawrect 來繪制的瘤礁,對應(yīng)類是MBBarProgressView 阳懂,determinanteMode
對應(yīng)類MBRoundProgressView。
總結(jié)
優(yōu)點(diǎn)
- MB 的View層級靈活柜思,可以放到不同的View 或者Window 中岩调,而SVPoregress卻不行
- MB 的CustomView可以自定義視圖,相對比SVProgress定制性要高
缺點(diǎn)
1赡盘、 MB 的動畫用的是drawrect 而非CALayer 相對沒那么高效