iOS MBProgressHUD 源碼泛讀

<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];
}

代碼整體難度不大,但是在項目中使用廣泛。

我們的項目從頭到尾只使用了菊花和問題的樣式,所以,進度條那些完全可以自己剔除掉。個人愚見。。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 欄目緣起:慢私塾「有一個朋友」領受天命,純粹的宇宙意識從那光芒萬丈的源頭出發,暖意融融,只是流淌。流淌。流淌。衷心...
    dc012dc63302閱讀 329評論 0 0
  • 我們每個人對抗的 都是全世界。 你順從自己的內心?還是順應外部的環境? 似乎有點唯心主義了,人都是順從世界,然后擁...
    4d8ef48cfd78閱讀 213評論 1 1
  • 根據輸入框內的時間日期,獲得該日期前6天的日期; 得到該時間近一周的日期: 我原先寫了得到前一天的函數: 在使用過...
    httIsHere閱讀 290評論 0 0
  • “學而則思不罔,思而則學不殆”。一本書能否發揮它本身的益處,就要看讀者對它的感悟程度。 ...
    一處落英閱讀 374評論 0 1