首页 > 代码库 > 网络等待动画
网络等待动画
实现的效果如图:
先上代码:
//// LSAnimationView.h// KaTestDemo2.0//// Created by Lisa on 14-12-17.// Copyright (c) 2014年 Lisa. All rights reserved.//#import <UIKit/UIKit.h>@interface LSAnimationView : UIView@property (nonatomic, readonly) BOOL isAnimating;@property(nonatomic,assign)CGFloat animationTime;-(void)startImaegeAnimaition;- (void)stopAnimationWithType:(BOOL)type;@end
//// LSAnimationView.m// KaTestDemo2.0//// Created by Lisa on 14-12-17.// Copyright (c) 2014年 Lisa. All rights reserved.//#import "LSAnimationView.h"@interface LSAnimationView (){ UIImageView *imageView; UILabel *Infolabel; UIButton *againButton; UILabel *orzLabel; NSTimer *_startTimer; NSTimer *_endTimer; CGFloat _selfY; CGFloat _selfX; CGFloat _selfWidth; CGFloat _selfHeight; BOOL _isSucess;}@end@implementation LSAnimationView-(id)init{ if (self = [super init]) { self.frame = CGRectMake([UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height/2 - 55, 160, 65); self.backgroundColor = [UIColor clearColor]; _selfX = self.frame.origin.x; _selfY = self.frame.origin.y; _selfWidth = self.frame.size.width; _selfHeight = self.frame.size.height; _isAnimating = NO; if (imageView == nil) { imageView = [UIImageView new]; [self addSubview:imageView]; } if(Infolabel == nil) { Infolabel = [[UILabel alloc]initWithFrame:CGRectMake(10,_selfHeight-30, _selfWidth-40, 15)]; Infolabel.backgroundColor = [UIColor clearColor]; Infolabel.text = @"加载中..."; Infolabel.textAlignment = NSTextAlignmentCenter; Infolabel.textColor = [UIColor colorWithRed:66.0/255.0 green:66.0/255.0 blue:66.0/255.0 alpha:1.0]; Infolabel.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14.0f]; [self addSubview:Infolabel]; Infolabel.hidden = YES; } if (orzLabel == nil) { orzLabel = [[UILabel alloc]initWithFrame:CGRectMake(85, 15, 80, 20)]; orzLabel.backgroundColor = [UIColor clearColor]; orzLabel.text = @"囧rz...过不去ing"; orzLabel.textColor = [UIColor colorWithRed:66.0/255.0 green:66.0/255.0 blue:66.0/255.0 alpha:1.0]; orzLabel.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:12.0f]; orzLabel.hidden = YES; [self addSubview:orzLabel]; } if(againButton == nil) { againButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self addSubview:againButton]; againButton.backgroundColor = [UIColor whiteColor]; againButton.titleLabel.font = [UIFont systemFontOfSize:14.0f]; [againButton setTitleColor:[UIColor colorWithRed:66.0/255.0 green:66.0/255.0 blue:66.0/255.0 alpha:1.0] forState:UIControlStateNormal]; againButton.layer.cornerRadius = 5; [againButton setTitle:@"我再试一次" forState:UIControlStateNormal]; againButton.frame = CGRectMake(20, _selfHeight - 25, _selfWidth - 50, 20); [againButton addTarget:self action:@selector(buttonActtion:) forControlEvents:UIControlEventTouchUpInside]; againButton.hidden = YES; } if (_startTimer == nil) { _startTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeImageFrameWith:) userInfo:@"start" repeats:YES]; [_startTimer fire]; } [_startTimer setFireDate:[NSDate distantFuture]]; } return self;}-(void)buttonActtion:(UIButton *)button{ orzLabel.hidden = YES; againButton.hidden = YES; imageView.frame = CGRectMake(30, 0, 90, 40); imageView.image = [UIImage imageNamed:@"sucess_3"]; [self startAnimation];}-(void)startImaegeAnimaition{ imageView.image = [UIImage imageNamed:@"sucess_3"]; Infolabel.hidden = YES; orzLabel.hidden = YES; againButton.hidden = YES; self.frame = CGRectMake(_selfX, _selfY, _selfWidth, _selfHeight); imageView.frame = CGRectMake(30, 0, 90, 40); [_startTimer setFireDate:[NSDate date]];}-(void)changeImageFrameWith:(NSTimer *)flagTimer{ NSString *flag =(NSString *)flagTimer.userInfo; CGFloat imageX= self.frame.origin.x; CGFloat space = 10; if ([flag isEqualToString:@"start"]) { if (imageX <=[UIScreen mainScreen].bounds.size.width/2-_selfWidth/2 + 10) { [_startTimer setFireDate:[NSDate distantFuture]]; [self startAnimation]; return; } } if ([flag isEqualToString:@"end"]) { if (_isSucess) { if (imageX <= -_selfWidth) { [_endTimer setFireDate:[NSDate distantFuture]]; return; } } else{ [_endTimer setFireDate:[NSDate distantFuture]]; if (imageView.isAnimating == NO) { [imageView startAnimating]; } [self performSelector:@selector(imageStop) withObject:nil afterDelay:imageView.animationDuration]; return; } } self.frame = CGRectMake(imageX - space, self.frame.origin.y, _selfWidth, _selfHeight); }-(void)imageStop{ [imageView stopAnimating]; [imageView setImage:[UIImage imageNamed:@"fail_9"]]; orzLabel.hidden = NO; againButton.hidden = NO;}- (void)startAnimation{ //设置动画帧 imageView.animationImages=[NSArray arrayWithObjects: [UIImage imageNamed:@"sucess_1"], [UIImage imageNamed:@"sucess_2"], [UIImage imageNamed:@"sucess_3"], [UIImage imageNamed:@"sucess_4"], [UIImage imageNamed:@"sucess_5"], [UIImage imageNamed:@"sucess_6"], nil ]; _isAnimating = YES; [self doAnimation];}-(void)doAnimation{ Infolabel.hidden = NO; //设置动画总时间 imageView.animationDuration=1.0; if (self.animationTime) { imageView.animationDuration = self.animationTime; } //设置重复次数,0表示不重复 imageView.animationRepeatCount=0; //开始动画 [imageView startAnimating];}- (void)stopAnimationWithType:(BOOL)type{ Infolabel.hidden = YES; _isSucess = type; _isAnimating = NO; if(type){ [UIView animateWithDuration:0.3f animations:^{ // self.alpha = 0; } completion:^(BOOL finished) { [imageView stopAnimating]; [self setUpEndTimer]; }]; }else{ [imageView stopAnimating]; [self setUpEndTimer]; } }-(void)setUpEndTimer{ if (_isSucess == NO) { imageView.frame = CGRectMake(0, 0, 120, 40);
//设置动画帧 imageView.animationImages=[NSArray arrayWithObjects: [UIImage imageNamed:@"fail_1"], [UIImage imageNamed:@"fail_2"], [UIImage imageNamed:@"fail_3"], [UIImage imageNamed:@"fail_4"], [UIImage imageNamed:@"fail_5"], [UIImage imageNamed:@"fail_6"], [UIImage imageNamed:@"fail_7"], [UIImage imageNamed:@"fail_8"], [UIImage imageNamed:@"fail_9"], nil ]; } if (_endTimer == nil) { _endTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeImageFrameWith:) userInfo:@"end" repeats:YES]; [_endTimer fire]; } else{ [_endTimer setFireDate:[NSDate date]]; } }-(void)setAnimationTime:(CGFloat)animationTime{ _animationTime = animationTime;}-(void)dealloc{ Infolabel = nil; imageView = nil; [_startTimer invalidate]; [_endTimer invalidate];}@end
使用代码:
// ViewController.m// AnimationVIewDemo//// Created by Lisa on 14-12-17.// Copyright (c) 2014年 Lisa. All rights reserved.//#import "ViewController.h"#import "LSAnimationView.h"@interface ViewController (){ LSAnimationView *_animationView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _animationView = [[LSAnimationView alloc]init]; [self.view addSubview:_animationView];}- (IBAction)buttonAction:(UIButton *)sender { int tag = (int)sender.tag; //tag 100 开始 101 成功 102 失败 switch (tag) { case 100: [_animationView startImaegeAnimaition]; break; case 101: [_animationView stopAnimationWithType:YES]; break; case 102: [_animationView stopAnimationWithType:NO]; break; default: break; } }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
PS:这个动画实现主要依赖于UIImageview自身的animationImages属性,实现入门级别的动画效果还是可以的。
如果图片少的话也许这种方式是最快速最容易达到目的的,但是图片很多的话,根据目前我做的实验,图片很多的话 这种方式程序必须会崩溃的。
而且动画不能够实现暂停,只有停止。所以想要实现高性能、酷炫的动画效果还是需要再学习其他知识的!
与君共勉!
网络等待动画
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。