【仿去哪儿】骆驼动画加载

简单的实现正在加载的动画,比较粗糙,没有实现后面的旋转地球。图一为去哪儿的截图,图二为本文实现的效果




图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];




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值