首页 > 代码库 > 使用Objective-c实现Tom猫《二》

使用Objective-c实现Tom猫《二》

2.选择 createfolder references for any folders,文件夹是蓝色,运行程序时,他会在Bundel中再细分文件夹(以下图为例:运行程序后会在Bundel包中再细分Animations/……)

*/把素材(素材存放在Animations中,Animations又细分Angry,Drink…)从finder中拉倒Supporting Files,按如下图所示打钩,



1
#import "LFViewController.h" 2 3 @interface LFViewController () 4 5 @property (weak, nonatomic) IBOutlet UIImageView *tom; 6 7 @end 8 9 @implementation LFViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 15 NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Animations"]; 16 NSString *filePath = [NSString stringWithFormat:@"%@/Drink/drink_00.jpg", bundlePath]; 17 18 self.tom.image = [UIImage imageWithContentsOfFile:filePath]; 19 } 20 21 - (void)runAnimationWithName:(NSString *)name 22 { 23 if (self.tom.isAnimating) return; 24 25 // 1. 设置Tom的动画数组 26 NSMutableArray *arrayM = [NSMutableArray array]; 27 // 获取Animatiosns路径 28 NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Animations"]; 29 // 追加路径 30 NSString *animPath = [bundlePath stringByAppendingPathComponent:name]; // 假如传的name值为Drink,则animPath值为 bundel路径/Animations/Drink
31 32 // NSFileManager是专门用来做磁盘文件管理的 33 // 取出指定目录中的所有文件 34 NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:animPath error:NULL]; 35 36 for (NSString *fileName in files) { 37 NSString *filePath = [NSString stringWithFormat:@"%@/%@", animPath, fileName]; 38 UIImage *image = [UIImage imageWithContentsOfFile:filePath]; 39 40 [arrayM addObject:image]; 41 } 42 43 self.tom.animationImages = arrayM; 44 self.tom.animationDuration = arrayM.count * 0.075; 45 self.tom.animationRepeatCount = 1; 46 47 // 2. 开始播放 48 [self.tom startAnimating]; 49 NSLog(@"%f", self.tom.animationDuration); 50 // 3. 清空动画数组 51 [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration]; 52 } 53 54 - (IBAction)tomAction:(UIButton *)button 55 { 56 [self runAnimationWithName:button.currentTitle]; // 先在storyboard中设置每个按钮的title,然后才能正确传参(title要跟文件夹名相同) 57 } 58 59 @end