MBProgressHUD

[TOC]

MBProgressHUD 是iOS 第三方Hub 框控件。

结构

控件组成

在MBProgressHUD(以下简称MB)中hub 的结构是由几部分组成的

  • indicatorView 展示菊花,进度条等样式的视图。根据不同mode来创建不同的View。
  • backgroundView背景View,处理暗色,或者模糊样式,对应类MBBackgroundView
typedef NS_ENUM(NSInteger, MBProgressHUDBackgroundStyle) {
    MBProgressHUDBackgroundStyleSolidColor,
    MBProgressHUDBackgroundStyleBlur
};
  • label 加载框下方的描述信息
  • detailsLabel 描述信息下面的详细描述信息
  • actionButton 在提供一个操作的button

样式

在MBProgressHUD(以下简称MB)的头文件中描述了几种样式

typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
    /// 无限菊花样式.
    MBProgressHUDModeIndeterminate,
    /// 圆形进度条样式.
    MBProgressHUDModeDeterminate,
    /// 横向滚动进度条样式.
    MBProgressHUDModeDeterminateHorizontalBar,
    /// 圆形进度条样式.和第二个有点不同
    MBProgressHUDModeAnnularDeterminate,
    /// 自定义View的样式
    MBProgressHUDModeCustomView,
    /// 只有文字的样式.
    MBProgressHUDModeText
};

MB 通过MBProgressHUDMode来创建不同展示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];
}

具体实现

MB 的实现比较朴实,没有过多花哨的技巧,简单实用。
展示的流程大概是这样的

  1. 通过initWithWindowinitWithView来创建Hub ,这里的Window和View不是MB 来添加的父视图而是算确定frame的视图
  2. 通过showHUDAddedTo来将Hub 添加到指定的父视图。
  3. 调用showAnimated通过指定动画来展示出Hub
  4. 实用hideAnimated来指定动画消失

其他

MB 通过通知UIApplicationDidChangeStatusBarOrientationNotification来处理屏幕转屏事件

- (void)registerForNotifications {
#if !TARGET_OS_TV
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self selector:@selector(statusBarOrientationDidChange:)
               name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
#endif
}

MB 的MBProgressHUDModeDeterminateHorizontalBar模式是通过drawrect 来绘制的,对应类是MBBarProgressViewdeterminanteMode对应类MBRoundProgressView

总结

优点

  1. MB 的View层级灵活,可以放到不同的View 或者Window 中,而SVPoregress却不行
  2. MB 的CustomView可以自定义视图,相对比SVProgress定制性要高

缺点
1、 MB 的动画用的是drawrect 而非CALayer 相对没那么高效

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 源码来源:gitHub源码 转载于: CocoaChina 来源:南峰子的技术博客 版本:0.9.1 MBPr...
    李小六_阅读 6,564评论 2 5
  • - (void)testMBProgressHUD { NSLog(@"test MBProgressHUD ")...
    有偶像包袱的程序狗阅读 1,673评论 0 1
  • 前言 作为初学者,想要快速提高自己的水平,阅读一些优秀的第三方源代码是一个非常好的途径.通过看别人的代码,可以学习...
    __微凉阅读 49,012评论 16 203
  • 11月的最后一天 学习了社群营销知识 一个可以盈利的正确社群需要做到以下过程:社群策划-文案引流-群员筛选-运用社...
    candymoon阅读 142评论 0 0
  • 1.我没更新就是因为懒 上次写了篇《如果你有长长的望远镜,我希望你用它来教孩子们看星星》,第二天就被邀请开通...
    离人心上秋11阅读 325评论 0 1

友情链接更多精彩内容