// // ViewController.h // 07-TomCat // // Created by zjj on 15/5/8. // Copyright (c) 2015年 zjj. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UIImageView *tom; - (IBAction)btnClik:(UIButton *)sender; @end
1 // 2 // ViewController.m 3 // 07-TomCat 4 // 5 // Created by zjj on 15/5/8. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 { 13 NSDictionary *_dict; 14 } 15 @end 16 17 @implementation ViewController 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // 2.用NSBUndle 读取(tom.plist)文件全路径 21 NSBundle *bundle = [NSBundle mainBundle]; 22 NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"]; 23 //1.根据文件路径加载字典 24 _dict = [NSDictionary dictionaryWithContentsOfFile:path]; 25 } 26 27 - (void)btnClik:(UIButton *)sender 28 { 29 // 是否正在播放动画,是则点击其他按钮无效 30 if(_tom.isAnimating)return; 31 // 3.取出按钮文字(按钮文字事先要clean title clocr) 32 NSString *title = [sender titleForState:UIControlStateNormal]; 33 int count = [_dict[title]intValue]; 34 [self catChange:count str:title]; 35 } 36 - (void)catChange : (int)count str:(NSString *)playName 37 { 38 // 1.创建可变数组 39 NSMutableArray *img = [NSMutableArray array]; 40 // 2.添加图片 41 for (int i = 0 ; i < count; i++) { 42 //%02d 保留2位多余的用0代替 如01-09 省却if判断 43 NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg",playName,i]; 44 // 加载缓存方法 大批量图片之类 会占用大量内存(内存检测泄露工具)有缓存 却无法释放 45 // UIImage *name = [UIImage imageNamed:path]; 46 // 系统优化 47 // 无缓存 用完就释放 48 NSString *paths = [[NSBundle mainBundle] pathForResource:name ofType:nil]; 49 UIImage *imgs = [[UIImage alloc]initWithContentsOfFile:paths]; 50 [img addObject:imgs]; 51 } 52 // 3.设置动画图片 53 _tom.animationImages = img;// 序列帧图画 54 // 4.只播放一次 55 _tom.animationRepeatCount = 1; 56 // 5.设置动画的持续时间 57 _tom.animationDuration = 0.1 * count; 58 // 6.开始播放动画 59 [_tom startAnimating]; 60 } 61 @end
原文:http://www.cnblogs.com/zhangdashao/p/4489094.html