加载动画

#import <UIKit/UIKit.h>


@interface YYAnimationIndicator : UIView


{

    NSTimer *timer;

    UILabel *backView;

    DeviceManage * device;

}

@property(nonatomic,strong)UIImageView *imageView;;

@property(nonatomic,strong)UIView * bgView;

@property(nonatomic,strong) UILabel *Infolabel;;

@property (nonatomic, assign) NSString *loadtext;

@property (nonatomic, readonly) BOOL isAnimating;

@property(nonatomic,assign)CGFloat topY;



//use this to init

- (id)initWithFrame:(CGRect)frame;

- (id)initWithFrame:(CGRect)frame andTopY:(CGFloat)topY;

- (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor *)bgColor;

-(void)setLoadText:(NSString *)text;


- (void)startAnimation;

- (void)stopAnimationWithLoadText:(NSString *)text withType:(BOOL)type;


-(void)createCustomAnimation;


@end

#define kBgWH 170

#define KImgaeWH 220

#define KtopY 150

#import "YYAnimationIndicator.h"

#define WidthScale ([UIScreen mainScreen].bounds.size.width/375)

#define HeightScale ([UIScreen mainScreen].bounds.size.height/667)


@implementation YYAnimationIndicator


- (id)initWithFrame:(CGRect)frame

{

    self = [self initWithFrame:frame backgroundColor:[UIColor whiteColor]];

    return self;

}


- (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor *)bgColor

{

    self = [super initWithFrame:frame];

    if (self) {

        device = [DeviceManage deviceManage];

        self.backgroundColor = bgColor;

        _isAnimating = NO;

        [self addSubview:self.imageView];

        self.layer.hidden = YES;

    }

    return self;

}

- (id)initWithFrame:(CGRect)frame andTopY:(CGFloat)topY

{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor whiteColor];

        device = [DeviceManage deviceManage];

        _isAnimating = NO;

        self.topY = topY;

        [self addSubview:self.imageView];

        self.layer.hidden = YES;

    }

    return self;

}


-(void)startAnmi

{

    [self stopAnimationWithLoadText:@"加载失败" withType:NO];

    [timer invalidate];

//    [self removeFromSuperview];

}


- (void)startAnimation

{

    _isAnimating = YES;

    self.layer.hidden = NO;

    timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(startAnmi) userInfo:nil repeats:NO];

    [self doAnimation];

}


-(void)doAnimation{

    

    self.Infolabel.text = _loadtext;

    //设置动画总时间

    self.imageView.animationDuration=2.3;

    //设置重复次数,0表示不重复

    self.imageView.animationRepeatCount=0;

    //开始动画

    [self.imageView startAnimating];

}


- (void)stopAnimationWithLoadText:(NSString *)text withType:(BOOL)type;

{

    _isAnimating = NO;

    self.Infolabel.text = text;

    if(type){

        

        [UIView animateWithDuration:0.3f animations:^{

            self.alpha = 0;

        } completion:^(BOOL finished) {

            [self.imageView stopAnimating];

            self.layer.hidden = YES;

            self.alpha = 1;

        }];

        [self removeFromSuperview];

    }else{

        [self removeFromSuperview];

        [self.imageView stopAnimating];

        [self.imageView setImage:[UIImage imageNamed:@"loading_0005"]];

    }

    

}


-(void)setLoadText:(NSString *)text;

{

    if(text){

        _loadtext = text;

        [self insertSubview:self.bgView belowSubview:self.imageView];

        [self addSubview:self.Infolabel];

    }

}


-(UILabel *)Infolabel

{

    if (_Infolabel == nil) {

        _Infolabel = [[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.imageView.frame)-60*device.heighScaleRatio,ScreenWidth, 20)];

        _Infolabel.textAlignment = NSTextAlignmentCenter;

        _Infolabel.textColor = UIColorWithRGB(0xEE4F4E);

        _Infolabel.font = [UIFont systemFontOfSize:15.0];

        }

    return _Infolabel;

}

-(UIView *)bgView{

    if (_bgView == nil) {

        if (self.topY) {

            _bgView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-kBgWH*device.widthScaleRatio)/2,self.topY*device.heighScaleRatio, kBgWH*device.widthScaleRatio, kBgWH*device.widthScaleRatio)];

        }else

        {

        _bgView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-kBgWH*device.widthScaleRatio)/2,(KtopY+50)*device.heighScaleRatio, kBgWH*device.widthScaleRatio, kBgWH*device.widthScaleRatio)];

        }

        _bgView.backgroundColor = [UIColor clearColor];

        _bgView.alpha = 0.5;

        _bgView.layer.cornerRadius = 5;

        _bgView.layer.masksToBounds = YES;


    }

    return _bgView;

}

-(UIImageView *)imageView

{

    if (_imageView == nil) {

        if (self.topY) {

             _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((ScreenWidth-KImgaeWH*device.widthScaleRatio)/2,self.topY*device.heighScaleRatio, KImgaeWH*device.widthScaleRatio, KImgaeWH*device.widthScaleRatio)];

        }else

        {

             _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((ScreenWidth-KImgaeWH*device.widthScaleRatio)/2,KtopY*device.heighScaleRatio,  KImgaeWH*device.widthScaleRatio, KImgaeWH*device.widthScaleRatio)];

        }

        //设置动画帧

        NSMutableArray *arr = [[NSMutableArray alloc]init];

        for (int i = 0; i<=58; i++)

        {

            NSString *str =[ [NSString alloc]initWithFormat:@"loading_000%02d",i ];

            UIImage *image = [UIImage imageNamed:str];

            [arr addObject:image];

        }

        _imageView.contentMode = UIViewContentModeScaleAspectFill;

        _imageView.clipsToBounds = YES;

        _imageView.animationImages =(NSArray *)arr;

    }

    return _imageView;

}

-(void)createCustomAnimation

{

    self.backgroundColor = [UIColor clearColor];

    backView.backgroundColor = [UIColor clearColor];

}


@end



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值