發(fā)現(xiàn)在使用MB的自定義動畫的時候,如果不封裝一下每個地方就會多出一堆的重復(fù)代碼,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
現(xiàn)在開始封裝:
1辽社、創(chuàng)建一個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文件中
創(chuàng)建全局的MBProgressHUD
@interface CustomShowHud ()<MBProgressHUDDelegate>
{
MBProgressHUD *_hud;
}
@end
創(chuàng)建 - (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;
}
創(chuàng)建- (void)animationShow:(BOOL)show汉匙,用于展示,由于我項目中自定義hud的動畫只有一種生蚁,so我就將整塊給封裝到里面噩翠,在需要的地方直接一個調(diào)用:
- (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];
}
實現(xiàn)hud代理MBProgressHUDDelegate:
- (void)hudWasHidden:(MBProgressHUD *)hud
{
[_hud removeFromSuperview];
_hud = nil;
}
實現(xiàn)+ (instancetype)showCustomHudText:(NSString *)text textColor:(UIColor *)color InView:(UIView *)view,用于創(chuàng)建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伤锚、調(diào)用
創(chuàng)建一個全局的hud:
@property (nonatomic, strong) CustomShowHud * hud;
需要的地方加上代碼:
_hud = [CustomShowHud showCustomHudText:@"努力加載中..." textColor:COLOR_153 InView:self.view];
然后不需要的時候隱藏。
總的就這些了志衣,如果需要設(shè)置一些別的可以參考來源這里见芹,好了就這些。