MBProgressHUD自定义动画封装

发现在使用MB的自定义动画的时候,如果不封装一下每个地方就会多出一堆的重复代码,So找了一下百度,将原本未封装的代码封装了下,原本未封装的自定义动画代码如下:

   //自定义view
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    //取消背景框
    self.hud.color = [UIColor clearColor];
    [self.view addSubview:_hud];
    UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCR_W * 50, SCR_W * 50)];
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    for(int i = 0; i < 2 ; i++){
        NSString *imgName = [NSString stringWithFormat:@"loading%d",i];
        [imageArray addObject:[UIImage imageNamed:imgName]];
    }
    images.animationDuration = 0.3;
    images.animationImages = imageArray;
    // 开始播放
    [images startAnimating];
    //自定义
    _hud.mode = MBProgressHUDModeCustomView;
    _hud.delegate = self;
    _hud.customView = images;
    _hud.labelText = @"努力加载中...";
    _hud.labelColor = COLOR_153;//宏定义的字体颜色
    [_hud show:YES];

先上一波效果图


自定义动画刷新.gif

现在开始封装:

1、创建一个NSObject的类CustomShowHud:

在CustomShowHud.h文件中,记得引入MBProgressHud的头文件MBProgressHUD.h,定义自己所需要的属性,例如:

@interface CustomShowHud : NSObject
@property (nonatomic, assign) MBProgressHUDAnimation animationStyle;
@property (nonatomic, copy) NSString * text;//显示的文字
@property (nonatomic, copy) UIColor * textColor;//显示的文字颜色
    + (instancetype)showCustomHudText:(NSString *)text textColor:(UIColor *)color InView:(UIView *)view;
    - (void)animationShow:(BOOL)show;
    - (void)hide:(BOOL)hide;
@end

2、在CustomShowHud.m文件中

创建全局的MBProgressHUD

@interface CustomShowHud ()<MBProgressHUDDelegate>
{
    MBProgressHUD *_hud;
}
@end

创建 - (instancetype)initWithView:(UIView *)view,添加视图

- (instancetype)initWithView:(UIView *)view
{
    if (view == nil) {
        return nil;
    }
    self = [super init];
    if (self) {
        //自定义view
        _hud = [[MBProgressHUD alloc] initWithView:view];
        //取消背景框
        _hud.color = [UIColor clearColor];
        [view addSubview:_hud];
    }
    return self;
}

重写定义的属性,并赋值给hud

  - (MBProgressHUDAnimation)animationStyle
{
    return _animationStyle;
}

创建- (void)animationShow:(BOOL)show,用于展示,由于我项目中自定义hud的动画只有一种,so我就将整块给封装到里面,在需要的地方直接一个调用:

- (void)animationShow:(BOOL)show
{
    if (_text != nil && _text.length != 0) {
        _hud.labelText = _text;
    }
    if (_textColor) {
        _hud.labelColor = _textColor;
    }
    UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCR_W * 50, SCR_W * 50)];
    
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    
    for(int i = 0; i < 2 ; i++){
        NSString *imgName = [NSString stringWithFormat:@"loading%d",i];
        
        [imageArray addObject:[UIImage imageNamed:imgName]];
    }
    
    images.animationDuration = 0.3;
    
    images.animationImages = imageArray;
    // 开始播放
    [images startAnimating];
    
    //自定义
    _hud.mode = MBProgressHUDModeCustomView;
    
    _hud.delegate = self;
    
    _hud.customView = images;
    
    [_hud show:YES];
}

实现hud代理MBProgressHUDDelegate:

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [_hud removeFromSuperview];
    
    _hud = nil;
}

实现+ (instancetype)showCustomHudText:(NSString *)text textColor:(UIColor *)color InView:(UIView *)view,用于创建hud:

+ (instancetype)showCustomHudText:(NSString *)text textColor:(UIColor *)color InView:(UIView *)view
{
    CustomShowHud * hud = [[CustomShowHud alloc] initWithView:view];
    
    hud.text = text;
    
    hud.textColor = color;
    
    [hud animationShow:YES];
    
    return hud;
}

最后,就是隐藏了:

- (void)hide:(BOOL)hide
{
    [_hud hide:hide];
}

3、调用

创建一个全局的hud:

@property (nonatomic, strong) CustomShowHud * hud;

需要的地方加上代码:

_hud = [CustomShowHud showCustomHudText:@"努力加载中..." textColor:COLOR_153 InView:self.view];

然后不需要的时候隐藏。

总的就这些了,如果需要设置一些别的可以参考来源这里,好了就这些。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,192评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,522评论 7 249
  • 今天是周一,公司事情比较多,上午又把昨天晚上的手写稿整理成简书,感觉马不停息的样子。到下午四点多的时候,眼睛...
    旷谷幽兰一阅读 361评论 1 1
  • 孩童是可爱的,他们的心地那般美好,如水晶一般玲珑剔透,所以他们说出的话语,也是那么有趣,那么美好,那么温暖…… 牛...
    刘忙不盲阅读 281评论 0 1
  • 一句话总结这次的课程:利用系统思考,价值思维,黄金思维圈以及5大决策原则,9大思路产生原则来制定年目标。但本质上,...
    lynn0阅读 404评论 2 0