帧动画的完整实现: 代码直接演示

帧动画的完整实现:  

 直接上代码演示更加清晰

 1 帧动画完整代码实现:
 2 #import "ViewController.h"
 3 @interface ViewController ()
 4 
 5 @property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 //把相同的代码封装一下
12 -(void)beginAnimationWithImageCount:(int) a imageName:(NSString *)imageName
13 {
14 //调用isAnimating方法,判断动画是否在执行中,如果在必须执行完成之后再执行其他的动画,得有判断if
15 if (self.imageViewIcon.isAnimating)return;
16 
17 //1.把需要执行的动画图片设置到UIImageView(图片框)
18 NSMutableArray *array=[NSMutableArray array];
19 
20 for (int i=0; i<a; i++) {
21 NSString *image=[NSString stringWithFormat:@"%@%03d",imageName,i+1];
22 
23 // UIImage *img=[UIImage imageNamed:image]; 通过这种方式来加载的数据是有缓存的,
24 //如果用路径的方式创建,就不会有缓存
25 NSString *img_path=[[NSBundle mainBundle]pathForResource:image ofType:@"png"];
26 UIImage *img=[UIImage imageWithContentsOfFile:img_path]; //
27 //注意:这里如果没有吧素材加载到Supporting Files中时,会显示为空,所以用路径方式的时候,要把素材都加到Supporting Files中
28 
29 [array addObject:img];
30 }
31 
32 //把要执行的动画的图片数组 设置给图片框的animationImages属性
33 self.imageViewIcon.animationImages=array;
34 
35 //2.设置动画的持续时间 让每一张图片都执行0.1秒
36 self.imageViewIcon.animationDuration=0.1*self.imageViewIcon.animationImages.count;
37 
38 //3.设置动画的重复次数
39 self.imageViewIcon.animationRepeatCount=1;
40 
41 //4.启动动画
42 [self.imageViewIcon startAnimating];
43 
44 //等待动画执行完毕后,再清理内存 performSelector :set方法 withObject对象 afterDelay时间
45 [self.imageViewIcon performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageViewIcon.animationDuration];
46 
47 }

 

posted on 2016-01-02 00:42  DXSmile  阅读(446)  评论(0编辑  收藏  举报