MBProgressHUD源碼解析

做過iOS開發的同學應該都使用過或者了解MBProgressHUD這個第三方框架,由于它對外接口簡潔,只需要幾句代碼傳入類型就可以實現HUD功能。那么它內部是如何實現的呢,我們來一起看看!

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{

    // Do something useful in the background
    [self doSomeWork];

    // IMPORTANT - Dispatch back to the main thread. Always access UI
    // classes (including MBProgressHUD) on the main thread.
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
});

MBProgressHUD is an iOS drop-in class that displays a translucent HUD with an indicator and/or labels while work is being done in a background thread. The HUD is meant as a replacement for the undocumented, private UIKit UIProgressHUD with some additional features

MBProgressHUD只有兩個文件,.h和.m。首先在頭文件中,添加了幾個枚舉類型,MBProgressHUDModeMBProgressHUDAnimationMBProgressHUDBackgroundStyle 分別為HUD的現實樣式,動畫類型和背景樣式。MBProgressHUD是繼承自UIView
當調用showHUDAddedTo方法時,先會在commonInit方法里面把對應的View層初始化。然后根據系統版本號>=8.0和MBProgressHUDBackgroundStyle加載使用** UIVisualEffectView**實現背景虛化毛玻璃效果

MBProgressHUDBackgroundStyle style = self.style;
if (style == MBProgressHUDBackgroundStyleBlur) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 || TARGET_OS_TV
    if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0) {
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        [self addSubview:effectView];
        effectView.frame = self.bounds;
        effectView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.backgroundColor = self.color;
        self.layer.allowsGroupOpacity = NO;
        self.effectView = effectView;
    } 

值得注意的是在初始化_bezelView用到了UIInterpolatingMotionEffect,這是iOS7以后增加視差效果的API,這個就是當你打開Home頁時左右翻轉,背景會跟著偏移的效果。

    CGFloat effectOffset = 10.f;
    UIInterpolatingMotionEffect *effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    effectX.maximumRelativeValue = @(effectOffset);
    effectX.minimumRelativeValue = @(-effectOffset);

    UIInterpolatingMotionEffect *effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    effectY.maximumRelativeValue = @(effectOffset);
    effectY.minimumRelativeValue = @(-effectOffset);

    UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
    group.motionEffects = @[effectX, effectY];

    [bezelView addMotionEffect:group];

之后就是根據MBProgressHUDMode類型添加indicator到self.bezelView上,定制了MBRoundProgressViewMBBarProgressView等UIView的子類,都是在<pre>- (void)drawRect:(CGRect)rect ;</pre>使用Quartz2D進行繪圖

MBRoundProgressView

在drawRect方法中就是了圓環或圓形:

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
BOOL isPreiOS7 = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0;
// 圓環繪制
if (_annular) {
    
    // 繪制背景
    CGFloat lineWidth = isPreiOS7 ? 5.f : 2.f;
    UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
    processBackgroundPath.lineWidth = lineWidth;
    processBackgroundPath.lineCapStyle = kCGLineCapButt;    //曲線終點樣式
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGFloat radius = (self.bounds.size.width - lineWidth)/2;
    CGFloat startAngle = - ((float)M_PI / 2); // 90°
    CGFloat endAngle = (2 * (float)M_PI) + startAngle;
    //該方法將會從 currentPoint 添加一條指定的圓弧
    [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [_backgroundTintColor set];
    [processBackgroundPath stroke];
    // 繪制進度條
    UIBezierPath *processPath = [UIBezierPath bezierPath];
    processPath.lineCapStyle = isPreiOS7 ? kCGLineCapRound : kCGLineCapSquare;
    processPath.lineWidth = lineWidth;
    endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
    [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [_progressTintColor set];
    [processPath stroke];
} else {
    // Draw background
    CGFloat lineWidth = 2.f;
    CGRect allRect = self.bounds;
    CGRect circleRect = CGRectInset(allRect, lineWidth/2.f, lineWidth/2.f);
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    [_progressTintColor setStroke];
    [_backgroundTintColor setFill];
    CGContextSetLineWidth(context, lineWidth);
    if (isPreiOS7) {
        CGContextFillEllipseInRect(context, circleRect);
    }
    CGContextStrokeEllipseInRect(context, circleRect);
    // 90 degrees
    CGFloat startAngle = - ((float)M_PI / 2.f);
    // Draw progress
    if (isPreiOS7) {
        CGFloat radius = (CGRectGetWidth(self.bounds) / 2.f) - lineWidth;
        CGFloat endAngle = (self.progress * 2.f * (float)M_PI) + startAngle;
        [_progressTintColor setFill];
        //畫筆移動到該點開始畫線
        CGContextMoveToPoint(context, center.x, center.y);
        CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
        CGContextClosePath(context);//閉合曲線
        CGContextFillPath(context);//填充路徑
    } else {
        UIBezierPath *processPath = [UIBezierPath bezierPath];
        processPath.lineCapStyle = kCGLineCapButt;
        processPath.lineWidth = lineWidth * 2.f;
        CGFloat radius = (CGRectGetWidth(self.bounds) / 2.f) - (processPath.lineWidth / 2.f);
        CGFloat endAngle = (self.progress * 2.f * (float)M_PI) + startAngle;
        [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
        // Ensure that we don't get color overlaping when _progressTintColor alpha < 1.f.
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        [_progressTintColor set];
        [processPath stroke];
    }
 }
}

MBBarProgressView

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2);
//設置描邊顏色
CGContextSetStrokeColorWithColor(context,[_lineColor CGColor]);
//設置填充顏色
CGContextSetFillColorWithColor(context, [_progressRemainingColor CGColor]);

// 繪制背景
CGFloat radius = (rect.size.height / 2) - 2;
//畫筆移動到該點開始畫線
CGContextMoveToPoint(context, 2, rect.size.height/2);
//添加一個圓弧的上下文路徑
CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
//畫直線到該點
CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
CGContextFillPath(context);

// Draw border
CGContextMoveToPoint(context, 2, rect.size.height/2);
CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
CGContextStrokePath(context);

CGContextSetFillColorWithColor(context, [_progressColor CGColor]);
radius = radius - 2;
CGFloat amount = self.progress * rect.size.width;

// 當Progress在區域范圍中間
if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4)) {
    CGContextMoveToPoint(context, 4, rect.size.height/2);
    CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
    CGContextAddLineToPoint(context, amount, 4);
    CGContextAddLineToPoint(context, amount, radius + 4);
    
    CGContextMoveToPoint(context, 4, rect.size.height/2);
    CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
    CGContextAddLineToPoint(context, amount, rect.size.height - 4);
    CGContextAddLineToPoint(context, amount, radius + 4);
    
    CGContextFillPath(context);
}

// 當Progress在右邊圓角
else if (amount > radius + 4) {
    CGFloat x = amount - (rect.size.width - radius - 4);

    CGContextMoveToPoint(context, 4, rect.size.height/2);
    CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
    CGContextAddLineToPoint(context, rect.size.width - radius - 4, 4);
    CGFloat angle = -acos(x/radius);
    if (isnan(angle)) angle = 0;
    CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, M_PI, angle, 0);
    CGContextAddLineToPoint(context, amount, rect.size.height/2);

    CGContextMoveToPoint(context, 4, rect.size.height/2);
    CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
    CGContextAddLineToPoint(context, rect.size.width - radius - 4, rect.size.height - 4);
    angle = acos(x/radius);
    if (isnan(angle)) angle = 0;
    CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, -M_PI, angle, 1);
    CGContextAddLineToPoint(context, amount, rect.size.height/2);
    
    CGContextFillPath(context);
}

// 當Progress在左邊圓角
else if (amount < radius + 4 && amount > 0) {
    CGContextMoveToPoint(context, 4, rect.size.height/2);
    CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
    CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);

    CGContextMoveToPoint(context, 4, rect.size.height/2);
    CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
    CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
    
    CGContextFillPath(context);
 }
}

然后利用KVC來設置進度顯示:
- (void)setProgress:(float)progress {
if (progress != _progress) {
_progress = progress;
UIView *indicator = self.indicator;
if ([indicator respondsToSelector:@selector(setProgress:)]) {
[(id)indicator setValue:@(self.progress) forKey:@"progress"];
}
}
}

接下來就是顯示了,在showUsingAnimation方法中,使用CADisplayLink去刷新進度條,因為進度條改變的非常快,實時監控它可能會影響主線程。

self.progressObjectDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgressFromProgressObject)];

調用刷新使用KVC刷新UI
- (void)updateProgressFromProgressObject {
self.progress = self.progressObject.fractionCompleted;
}

最后就是隱藏的方法了,調用是相類似的,在動畫執行完成會調用done方法,用戶可以自己去監聽回調方法做自定義一些事件,有block也有delegate,MBProgressHUDDelegateMBProgressHUDCompletionBlock

- (void)done {
// Cancel any scheduled hideDelayed: calls
[self.hideDelayTimer invalidate];
[self setNSProgressDisplayLinkEnabled:NO];

if (self.hasFinished) {
    self.alpha = 0.0f;
    if (self.removeFromSuperViewOnHide) {
        [self removeFromSuperview];
    }
}
MBProgressHUDCompletionBlock completionBlock = self.completionBlock;
if (completionBlock) {
    completionBlock();
}
id<MBProgressHUDDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(hudWasHidden:)]) {
    [delegate performSelector:@selector(hudWasHidden:) withObject:self];
 }
}

這是我用CAReplicatorLayer 和UIInterpolatingMotionEffect寫的一個loadingView Demo MotionEffectDemo,歡迎大家互相交流學習!

寫在最后 MBProgressHUD僅僅使用了一個類就實現了強大的HUD功能(雖然內部有幾個子類),對外提供的接口也很簡潔,封裝的思想和自定義視圖的地方是值得學習的

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

推薦閱讀更多精彩內容

  • HUD在iOS中一般特指“透明提示層”,常見的有SVProgressHUD、JGProgressHUD、Toast...
    foolishBoy閱讀 1,178評論 0 2
  • 簡書博客已經暫停更新,想看更多技術博客請到: 掘金 :J_Knight_ 個人博客: J_Knight_ 個人公眾...
    J_Knight_閱讀 5,968評論 36 38
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 源碼來源:gitHub源碼 轉載于: CocoaChina 來源:南峰子的技術博客 版本:0.9.1 MBPr...
    李小六_閱讀 6,461評論 2 5
  • 前言 最近在看并發編程藝術這本書,對看書的一些筆記及個人工作中的總結。 Lock接口 鎖是用來控制多個線程訪問共享...
    二月_春風閱讀 517評論 0 0