#import<UIKit/UIKit.h>
@interface ZBLoadingView : UIView
/*
* @brief--加载信息
*/
@property (nonatomic,copy) NSString* loadingInfo;
/*
* @brief--记载图片的名字
*/
@property (nonatomic,copy) NSString* loadingImageName;
/*
* @brief--加载-单例
*/
+ (instancetype)sharedInstance;
@end
-------------------------.m文件-----------------------------------
#import "ZBLoadingView.h"
//#import "Masonry.h"
@interface ZBLoadingView()
{
UILabel* _loadingInfoLabel;//加载信息
UIImageView* _loadingImageView;//加载图片视图
UIActivityIndicatorView* _activityIndicator;
}
@end
@implementation ZBLoadingView
static ZBLoadingView* loadingView;
#pragma amrk---加载单例
+ (instancetype)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
loadingView = [[ZBLoadingView alloc]init];
});
return loadingView;
}
#pragma mark--构造方法
- (instancetype)initWithFrame:(CGRect)frame{
if (self=[super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.2];
//1、加载图片视图
_loadingImageView = [[UIImageView alloc]init];
// [self addSubview:_loadingImageView];
// if (!_loadingImageName) {
_loadingImageView.image = [UIImage imageNamed:@"refresh.png"];
NSMutableArray* imageArr = [NSMutableArray array];
for (NSInteger index = 0; index < 2; index++) {
NSString* imageName = [NSString stringWithFormat:@"refresh%ld",index];
[imageArr addObject:[UIImage imageNamed:imageName]];
}
_loadingImageView.animationImages = imageArr;
_loadingImageView.animationDuration = 0.5;
_loadingImageView.animationRepeatCount = 0;
[_loadingImageView startAnimating];
// }
//2、加进度条--
_activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[self addSubview:_activityIndicator];
//说明文本
_loadingInfoLabel = [[UILabel alloc]init];
[self addSubview:_loadingInfoLabel];
_loadingInfoLabel.textAlignment = NSTextAlignmentCenter;
_loadingInfoLabel.font = [UIFont systemFontOfSize:14];
_loadingInfoLabel.textColor = [UIColor yellowColor];
if (!_loadingInfo) {
_loadingInfoLabel.text = @"加载中";
}
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
_loadingImageView.backgroundColor = [UIColor yellowColor];
// [_loadingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.center.mas_equalTo(self);
// make.size.mas_equalTo(CGSizeMake(45, 45));
// }];//_activityIndicator
[_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self);
make.size.mas_equalTo(CGSizeMake(45, 45));
}];
_loadingInfoLabel.backgroundColor = [UIColor redColor];
[_loadingInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(_activityIndicator);
make.top.mas_equalTo(_activityIndicator.mas_bottom).mas_offset(18);
make.height.mas_equalTo(18);
}];
[_activityIndicator startAnimating];
}
- (void)setLoadingInfo:(NSString *)loadingInfo{
_loadingInfo = loadingInfo;
_loadingInfoLabel.text = _loadingInfo;
}
- (void)setLoadingImageName:(NSString *)loadingImageName{
_loadingImageName = loadingImageName;
_loadingImageView.image = [UIImage imageNamed:_loadingImageName];
}
---------------------------------------------------------
#import <Foundation/Foundation.h>
@interface ZBHUD : NSObject
/*
* @brief---展示加载图
*/
+ (void)showLoading;
/*
* @brief---展示加载图
* @brief---loadingMessage:加载信息
*/
+ (void)showLoadingWithMessage:(NSString *)loadingMessage;
/*
* @brief---移除加载视图
*/
+ (void)dismiss;
/*
* @brief---加载期间是否禁止用户操作-----默认禁止
* isEnable-YES允许 NO--禁止
*/
+ (void)enableUserInterfacetion:(BOOL)isEnable;
/*
* @brief---展示可控制用户交互的加载图
* isEnable-YES允许 NO:禁止
*/
+ (void)showLoadingWithEnableUserInteraction:(BOOL)isEnable;
/*
* @brief--loadingMessage-加载信息
* isEnable-YES允许 NO:禁止
*/
+ (void)showLoadingWithMessage:(NSString *)loadingMessage enableUserInteraction:(BOOL)isEnable;
-----------------------------.m文件-------------
#import "ZBHUD.h"
#import "ZBLoadingView.h"
@implementation ZBHUD
+ (void)showLoading{
[ZBHUD showLoadingWithMessage:nil];
}
#pragma mark----展示带说明信息的加载图
+ (void)showLoadingWithMessage:(NSString *)loadingMessage{
UIWindow* window = [[[UIApplication sharedApplication]delegate]window];
[window addSubview:[ZBLoadingView sharedInstance]];
[ZBLoadingView sharedInstance].loadingInfo = loadingMessage;
[[ZBLoadingView sharedInstance]mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
}
#pragma mark---移除视图
+ (void)dismiss{
[[ZBLoadingView sharedInstance]removeFromSuperview];
}
#pragma mark---加载期间,允许或禁止用户交互:isEnable-YES允许 NO:禁止
+ (void)enableUserInterfacetion:(BOOL)isEnable{
[ZBLoadingView sharedInstance].userInteractionEnabled = !isEnable;
}
#pragma mark-----展示可控制用户交互的加载图
+ (void)showLoadingWithEnableUserInteraction:(BOOL)isEnable{
[ZBHUD showLoading];
[ZBHUD enableUserInterfacetion:isEnable];
}
#pragma mark---展示可控制用户交互并且带说明信息的加载图
+ (void)showLoadingWithMessage:(NSString *)loadingMessage enableUserInteraction:(BOOL)isEnable{
[ZBHUD showLoadingWithMessage:loadingMessage];
[ZBHUD enableUserInterfacetion:isEnable];
}
@end