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 ()
{
    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];

然后不需要的时候隐藏。

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

你可能感兴趣的:(MBProgressHUD自定义动画封装)