简单的实现正在加载的动画,比较粗糙,没有实现后面的旋转地球。图一为去哪儿的截图,图二为本文实现的效果
图1
图2
一、新建LoadView继承自UIView,声明几个方法,这样在项目需要用到时,直接调用就行, LoadView .h文件如下:
@interface LoadView : UIView
- (EmptyView*)initWithTitle:(NSString *)title;
-(void)setStaticImageWithTitle:(NSString*)title;
-(void)hiddenImage:(id)Mainview;
@end
二、LoadView.m文件,其中BannerLoadingX.jpg就是骆驼的动画图片
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define ImageW 102
#define ImageH 70
#define LabelW 100
#define LabelH 30;
@implementation LoadView{
UIImageView *imageView;
UILabel *label;
}
#pragma mark 初始化Loading动画
- (EmptyView*)initWithTitle:(NSString *)title
{
self = [super init];
if (self) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH-ImageW)/2, 100, ImageW, ImageH)];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"BannerLoading1.jpg"],
[UIImage imageNamed:@"BannerLoading2.jpg"],
[UIImage imageNamed:@"BannerLoading3.jpg"],
[UIImage imageNamed:@"BannerLoading4.jpg"],
[UIImage imageNamed:@"BannerLoading5.jpg"],nil];
//为动画设置持续时间
imageView.animationDuration = 3.0;
//为默认的无限循环
imageView.animationRepeatCount = 0;
//开始播放动画
[imageView startAnimating];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 228, WIDTH,30)];
[label setText:title];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor grayColor]];
[self addSubview:imageView];
[self addSubview:label];
}
return self;
}
#pragma mark 加载失败时调用,显示加载失败图片
-(void)setStaticImageWithTitle:(NSString*)title{
UIImage *image = [UIImage imageNamed:@"NetworkErrorHint"];
[imageView stopAnimating];
[imageView setImage:image];
[label setText:title];
}
#pragma mark 加载成功后调用,隐藏加载视图
-(void)hiddenImage:(id)Mainview{
self.hidden = YES;
}
@end
(1)#import "LoadView.h"
(2)声明一个LoadView类的实例view
(3)在需要显示Loading时调用:
view = [[EmptyView alloc] initWithTitle:@"正在努力加载!"];
(4)在加载失败时调用:
[view setStaticImageWithTitle:@"网络错误!"];
(5)在加载成功时调用:
[view hiddenImage:self];