#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