#import <UIKit/UIKit.h>
@interface LoadingView : UIView{
UIView *conerView;
}
///是否是模拟同步
@property (nonatomic) BOOL isLikeSynchro;
///显示加载框
- (void)show;
///关闭加载框
- (void)close;
///获取LoadingView单例,isLikeSynchro Yes:类似同步,通过遮盖整个窗体实现 No:异步
+ (LoadingView *)shareLoadingView;
@end
#define VIEW_HEIGHT [UIScreen mainScreen].bounds.size.height
#define VIEW_WIDTH [UIScreen mainScreen].bounds.size.width
#import "LoadingView.h"
#import "Header.h"
#import "AppDelegate.h"
@implementation LoadingView
static LoadingView *mLoadingView = nil;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
///初始化加载框,这个函数是表示LoadingView的大小,如果是Yes,则loadView的大小为整个窗体,在这种情况下网络请求的时候会遮盖整个窗体,用户其他操作都是无效的相当于同步,如果是No,则loadView的大小为为150*80,用户的其他操作是有效的,这种情况相下需要保证loadingView唯一;
- (id)initIsLikeSynchro:(BOOL)isLikeSynchro{
if (isLikeSynchro) {
self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
}else{
self = [super initWithFrame:CGRectMake((320-150)/2, ([UIApplication sharedApplication].keyWindow.bounds.size.height-80)/2, 150, 80)];
}
if (self) {
self.isLikeSynchro = isLikeSynchro;
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor clearColor];
conerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
[self setCenter:conerView withParentRect:self.frame];
UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.15];
conerView.backgroundColor = color;
[self addSubview:conerView];
UIImageView *refreshImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , 45, 45)];
refreshImgView.center = conerView.center;
NSMutableArray *imgArray = [[NSMutableArray alloc]init];
for (int i = 1; i < 34; i ++)
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"loading_1_gif%d",i] ofType:@"png"];
[imgArray addObject:[UIImage imageWithContentsOfFile:filePath]];
}
refreshImgView.animationImages = imgArray;
refreshImgView.animationDuration = 3;
refreshImgView.animationRepeatCount = 0;
[conerView addSubview:refreshImgView];
[refreshImgView startAnimating];
// conerView.layer.cornerRadius = 8;
// conerView.layer.masksToBounds = YES;
}
return self;
}
- (void)show{
if ([UIApplication sharedApplication].keyWindow.rootViewController.navigationController) {
[[UIApplication sharedApplication].keyWindow.rootViewController.navigationController.view addSubview:self];
}else{
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
}
- (void)close{
[self removeFromSuperview];
}
+ (LoadingView *)shareLoadingView{
@synchronized(self){
if (mLoadingView==nil) {
mLoadingView = [[self alloc] initIsLikeSynchro:YES];
}
}
return mLoadingView;
}
+ (id)allocWithZone:(NSZone *)zone{
@synchronized(self){
if (mLoadingView==nil) {
mLoadingView = [super allocWithZone:zone];
return mLoadingView;
}
}
return nil;
}
//设置子View在父View中居中
- (void)setCenter:(UIView *)child withParentRect:(CGRect)parentRect{
CGRect rect = child.frame;
rect.origin.x = (parentRect.size.width - child.frame.size.width)/2;
rect.origin.y = (parentRect.size.height - child.frame.size.height)/2;
child.frame = rect;
}