<code>MBProgressHUD</code>源碼通讀。
<code>MBProgressHUD</code>是一個簡單的在app里面彈出tost的開源庫,可以顯示進度條,菊花,文字,等等。直接看<code>git</code>就很容易上手使用。
因為項目中使用到了,敢完一期需求巴拉巴拉一下源碼。
<code>.h</code>文件中的定義。
///顯示樣式
MBProgressHUDMode
///動畫樣式
MBProgressHUDAnimation
///view 定義
MBProgressHUD
///條狀進度條定義
MBBarProgressView
///環狀進度條定義
MBRoundProgressView
應用中如果有要使用進度條的的地方,可以直接從源碼中摳出<code>MBBarProgressView</code>或者<code> MBRoundProgressView </code>修改使用。
先 巴拉一下進度條的實現,比如:<code>MBBarProgressView</code>
定義如下:
@interface MBBarProgressView : UIView
//進度 0 到 1 之間
@property (nonatomic, assign) float progress;
//邊線顏色
@property (nonatomic, MB_STRONG) UIColor *lineColor;
//背景色
@property (nonatomic, MB_STRONG) UIColor *progressRemainingColor;
//進度條的顏色
@property (nonatomic, MB_STRONG) UIColor *progressColor;
@end
實現比較簡單,默認的寬高是120 * 20
- (id)init {
return [self initWithFrame:CGRectMake(.0f, .0f, 120.0f, 20.0f)];
}
初始化方法只是簡單的設置一些默認值,還有注冊<code>kvo</code>
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_progress = 0.f;
_lineColor = [UIColor whiteColor];
_progressColor = [UIColor whiteColor];
_progressRemainingColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
[self registerForKVO];
}
return self;
}
進度條的實現是通過<code>drawRect</code>方法,畫上去的,有空可以細細的研究一番。
所有屬性變化都是通過kvo來監聽的,然后強制調用一下draw方法。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
- (void)registerForKVO {
for (NSString *keyPath in [self observableKeypaths]) {
[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
}
}
- (void)unregisterFromKVO {
for (NSString *keyPath in [self observableKeypaths]) {
[self removeObserver:self forKeyPath:keyPath];
}
}
- (NSArray *)observableKeypaths {
return [NSArray arrayWithObjects:@"lineColor", @"progressRemainingColor", @"progressColor", @"progress", nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[self setNeedsDisplay];
}
<strong>到此,一個簡單的進度條就實現了。</strong>以后可以借鑒,畢竟,這么人使用過。。。MBRoundProgressView 進度條實現方法基本一致,只是大小和 畫圖的地方不一樣。
<code> MBProgressHUD </code> 的組成
label
detailsLabel
Indicator
其中 <code>Indicator</code>可以是
UIActivityIndicatorView
MBBarProgressView
MBRoundProgressView
或者自定義的一個view
<code> MBProgressHUD </code> 并沒有使用 <code>autolayout</code>,而是使用 <code>frame</code> 直接設置位置, 位置的設置 是在 <code>layoutSubviews</code>中完成的,
布局完成后會記錄總共的使用的<code>size</code>
<code>self.size = totalSize;</code>這是為了繪制黑色背景部分記錄的。
繪制背景依舊在<code>drawRect</code>方法中。
屬性的改變也是通過kvo 來監聽的,變化了就調用布局和繪制方法,
- (void)updateUIForKeypath:(NSString *)keyPath {
if ([keyPath isEqualToString:@"mode"] || [keyPath isEqualToString:@"customView"]) {
[self updateIndicators];
} else if ([keyPath isEqualToString:@"labelText"]) {
label.text = self.labelText;
} else if ([keyPath isEqualToString:@"labelFont"]) {
label.font = self.labelFont;
} else if ([keyPath isEqualToString:@"detailsLabelText"]) {
detailsLabel.text = self.detailsLabelText;
} else if ([keyPath isEqualToString:@"detailsLabelFont"]) {
detailsLabel.font = self.detailsLabelFont;
} else if ([keyPath isEqualToString:@"progress"]) {
if ([indicator respondsToSelector:@selector(setProgress:)]) {
[(id)indicator setProgress:progress];
}
return;
}
[self setNeedsLayout];
[self setNeedsDisplay];
}
代碼整體難度不大,但是在項目中使用廣泛。
我們的項目從頭到尾只使用了菊花和問題的樣式,所以,進度條那些完全可以自己剔除掉。個人愚見。。