iOS开发:纯代码实现汤姆猫小游戏

本文介绍了一个使用纯代码实现汤姆猫动画效果的iOS应用案例。通过加载资源文件并利用UIButton和UIImageView创建交互界面,实现了汤姆猫的不同动画效果。文章详细展示了如何通过按钮点击触发动画播放,并管理图片资源。
摘要由CSDN通过智能技术生成

演示效果如下:

代码如下:

  1 //
  2 //  CKViewController.m
  3 //  纯代码实现汤姆猫
  4 //
  5 //  Created by FrankChen on 14-12-10.
  6 //  Copyright (c) 2014年 diaozhatian. All rights reserved.
  7 //
  8 
  9 #import "CKViewController.h"
 10 
 11 @interface CKViewController ()
 12 {
 13     // 保存所有图片的个数的数据字典
 14     NSDictionary *_imageDict;
 15 }
 16 // 汤姆猫view
 17 @property (nonatomic, weak) UIImageView *tomcat;
 18 
 19 @end
 20 
 21 @implementation CKViewController
 22 
 23 
 24 - (void)viewDidLoad
 25 {
 26     [super viewDidLoad];
 27     
 28     // 装载图片字典
 29     NSBundle *bundle = [NSBundle mainBundle];
 30     NSString *path = [bundle pathForResource:@"tomcat" ofType:@"plist"];
 31     _imageDict = [NSDictionary dictionaryWithContentsOfFile:path];
 32 
 33     // 创建汤姆猫的主界面显示图片
 34     UIImageView *mainImage = [[UIImageView alloc] init];
 35     [self.view addSubview:mainImage];
 36     self.tomcat = mainImage;
 37     CGFloat viewW = self.view.frame.size.width;
 38     CGFloat viewH = self.view.frame.size.height;
 39     mainImage.frame = CGRectMake(0, 0, viewW, viewH);
 40     mainImage.image = [UIImage imageNamed:@"angry_00.jpg"];
 41     
 42     // 创建头部按钮
 43     UIButton *headBtn = [[UIButton alloc] init];
 44     [self.view addSubview:headBtn];
 45     headBtn.frame = CGRectMake(56, 72, 208, 178);
 46     [headBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 47     [headBtn setTitle:@"knockout" forState:UIControlStateNormal];
 48     [headBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
 49     [headBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
 50     
 51     // 创建左脚部按钮
 52     UIButton *leftFoot = [[UIButton alloc] init];
 53     [self.view addSubview:leftFoot];
 54     leftFoot.frame = CGRectMake(162, 430, 46, 30);
 55     [leftFoot addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 56     [leftFoot setTitle:@"footLeft" forState:UIControlStateNormal];
 57     [leftFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
 58     [leftFoot setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
 59     
 60     // 创建右脚部按钮
 61     UIButton *rightFoot = [[UIButton alloc] init];
 62     [self.view addSubview:rightFoot];
 63     rightFoot.frame = CGRectMake(108, 430, 46, 30);
 64     [rightFoot addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 65     [rightFoot setTitle:@"footRight" forState:UIControlStateNormal];
 66     [rightFoot setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
 67     [rightFoot setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
 68     // 创建肚子部按钮
 69     UIButton *stomach = [[UIButton alloc] init];
 70     [self.view addSubview:stomach];
 71     stomach.frame = CGRectMake(106, 280, 104, 120);
 72     [stomach addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 73     [stomach setTitle:@"stomach" forState:UIControlStateNormal];
 74     [stomach setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
 75     [stomach setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
 76     // 创建尾巴部按钮
 77     UIButton *angry = [[UIButton alloc] init];
 78     [self.view addSubview:angry];
 79     angry.frame = CGRectMake(218, 390, 34, 50);
 80     [angry addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 81     [angry setTitle:@"angry" forState:UIControlStateNormal];
 82     [angry setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
 83     [angry setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
 84     
 85     // 采用循环的方式创建6个动画按钮
 86     CGFloat btnCount = 6;
 87     for (int index = 0; index < btnCount; index++) {
 88         NSArray *array = @[@"fart",@"eat",@"cymbal",@"pie",@"drink",@"scratch"];
 89         UIButton *btn = [[UIButton alloc] init];
 90         [self.view addSubview:btn];
 91         // 设置按钮的位置
 92         int totalColumns = 2; // 总列数
 93         // 1.图片的尺寸
 94         CGFloat imageW = 60;
 95         CGFloat imageH = 60;
 96         // 3.3.设置frame
 97         int row = index / totalColumns;
 98         int col = index % totalColumns;
 99         CGFloat marginX = viewW - 2 * imageW;
100         CGFloat marginY = 10;
101         // 计算x和y
102         CGFloat imageX = col * (imageW + marginX);
103         CGFloat imageY = 264 + row * (imageH + marginY);
104         btn.frame = CGRectMake(imageX, imageY, imageW, imageH);
105         // 给按钮设置title
106         [btn setTitle:array[index] forState:UIControlStateNormal];
107         // 让文字不可见
108         [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
109         [btn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
110         // 设置背景图片
111         UIImage *image = [UIImage imageNamed:array[index]];
112         [btn setBackgroundImage:image forState:UIControlStateNormal];
113         // 监听点击按钮事件
114         [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
115     }
116 }
117 
118 /**
119  *  播放动画
120  *
121  *  @param count 图片数
122  *  @param name  传入的图片前缀名
123  */
124 -(void)runAnimationWithCount:(int)count name:(NSString *)name{
125     if (self.tomcat.isAnimating) return;
126     // 1.加载所有的动画图片
127     NSMutableArray *images = [NSMutableArray array];
128     
129     for (int i = 0; i < count; i++) {
130         // 计算文件名
131         NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",name, i];
132         // 加载图片
133         NSBundle *bundle = [NSBundle mainBundle];
134         NSString *path = [bundle pathForResource:filename ofType:nil];
135         UIImage *image = [UIImage imageWithContentsOfFile:path];
136         // 添加图片到数组中
137         [images addObject:image];
138     }
139     self.tomcat.animationImages = images;
140     // 2.设置播放次数
141     self.tomcat.animationRepeatCount = 1;
142     // 3.设置播放时间
143     self.tomcat.animationDuration = images.count * 0.06;
144     // 4.开始播放
145     [self.tomcat startAnimating];
146     // 5.动画播放完毕1秒后将图片从内存中清除
147     CGFloat del = self.tomcat.animationDuration + 0.5;
148     [self.tomcat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:del];
149 }
150 /**
151  *  监听所有按钮点击事件
152  */
153 - (void)clickBtn:(UIButton *)sender{
154     // 取出按钮文字
155     NSString *title = [sender titleForState:UIControlStateNormal];
156     // 获得图片数量
157     int count = [_imageDict[title] intValue];
158     // 播放动画
159     [self runAnimationWithCount:count name:title];
160 }
161 
162 @end

 

转载于:https://www.cnblogs.com/qq920924960/p/4180667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值