码迷,mamicode.com
首页 > 其他好文 > 详细

TomcatGame设计(二)

时间:2015-11-18 15:48:05      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

tomcatGame 2

接着昨天的任务

1.因为该项目都是将图片进行轮播产生动画效果,其代码实现部分除了图片名字和数量不同外,其他都相同,为了提高重用性,简化编程,就将其重复代码封装为方法

 技术分享

2往imageView上托放6个button

技术分享 

3分别为每个button选择背景图片(cymbal,eat,drink,pie,scratch,fart),并去掉默认显示的title,并适当精确调节图片button具体位置

 技术分享

4打开assistant editor,分别拖动button到实现部分,并分别为实现方法命名(开头小写,遵循驼峰命名法则)

 技术分享

5方法实现:每个button方法分别调用封装好的tomAnimateWith: imageCount:方法,并且传入不同的参数

技术分享 

 

 1 //tomAnimateWith: imageCount: 方法代码
 2 - (void)tomAnimateWith:(NSString *)fileName imageCount:(NSInteger)imageCount
 3 {
 4     
 5         
 6         //检查是否正在动画运行,如果是则等执行完才执行新的
 7         if (self.tomImageView.isAnimating) {
 8             return;
 9         }
10         //1.首先创建图片数组
11         NSMutableArray *tomImages = [NSMutableArray array];//可变数组
12         
13         for (int i = 0; i < imageCount; i++) {
14             
15             NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",fileName,i];
16             
17             //创建图片的第一种方法(图片较多时会内存溢出,不建议用)
18             //        UIImage *image = [UIImage imageNamed:imageName];
19             
20             //创建图片的第二种方法,不会有内存溢出问题
21             NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
22             UIImage *image = [UIImage imageWithContentsOfFile:path];
23             
24             //每创建出一张图片就将其储存到可变数组中
25             [tomImages addObject:image];
26         }
27         
28         
29         //2.设置动画过程
30         //2.1添加动画执行的图片数组
31         [self.tomImageView setAnimationImages:tomImages];
32         //2.2设置动画执行的时间
33         [self.tomImageView setAnimationDuration:self.tomImageView.animationImages.count * 0.1];
34         //2.3设置动画执行次数(1次)
35         [self.tomImageView setAnimationRepeatCount:1];
36         //2.4设置动画开始执行
37         [self.tomImageView startAnimating];
38         
39         //3.动画结束后清空数组
40         //稍微容易理解的笨方法
41         //    [self performSelector:@selector(clearImages) withObject:nil afterDelay:self.tomImageView.animationDuration];
42         //直接让imageView重新执行一个setAnimationImages:方法
43         [self.tomImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tomImageView.animationDuration];
44     
45 }
46 //Button方法实现(调用tomAnimateWith: imageCount:方法)
47 - (IBAction)touchHeadOnClick:(UIButton *)sender {
48     
49     [self tomAnimateWith:@"knockout" imageCount:81];
50     
51 }
52 - (IBAction)touchCymbalOnClick {
53     
54     [self tomAnimateWith:@"cymbal" imageCount:13];
55 }
56 - (IBAction)touchDrinkOnClick {
57     
58     [self tomAnimateWith:@"drink" imageCount:81];
59 }
60 - (IBAction)touchEatOnClick {
61 
62     [self tomAnimateWith:@"eat" imageCount:40];
63 }
64 - (IBAction)touchFartOnClick {
65     
66     [self tomAnimateWith:@"fart" imageCount:28];
67 }
68 - (IBAction)touchScrathOnClick {
69     
70     [self tomAnimateWith:@"scratch" imageCount:56];
71 }
72 - (IBAction)touchPieOnClick {
73     
74     [self tomAnimateWith:@"pie" imageCount:24];
75 }

 

TomcatGame设计(二)

标签:

原文地址:http://www.cnblogs.com/starainDou/p/4974406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!