1 //
2 // LWTViewController.m
3 // 纯代码Tom
4 //
5 // Created by apple on 14-5-21.
6 // Copyright (c) 2014年 lwt. All rights reserved.
7 //
8
9 #import "LWTViewController.h"
10 #define KBtnSize 60
11
12 // 图标按钮的左右的排列
13 typedef enum {
14 KButtonThird = 1,
15 KButtonSecond,
16 KButtonFirst
17 }KButton;
18
19 @interface LWTViewController ()
20
21 // 保存plist数据
22 @property (nonatomic, strong) NSDictionary *dict;
23
24 // 全屏图片属性
25 @property (nonatomic, strong) UIImageView *tom;
26
27 @end
28
29 @implementation LWTViewController
30
31 // 懒加载plist文件
32 - (NSDictionary *)dict
33 {
34 if(!_dict)
35 {
36 NSBundle *bundle = [NSBundle mainBundle];
37 NSString *dictPath = [bundle pathForResource:@"Tom" ofType:@"plist"];
38 self.dict = [NSDictionary dictionaryWithContentsOfFile:dictPath];
39 }
40 return _dict;
41 }
42
43 - (void)viewDidLoad
44 {
45 [super viewDidLoad];
46 // Do any additional setup after loading the view, typically from a nib.
47
48 // 创建默认全屏图片View
49 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
50
51 // 加载图片
52 NSBundle *bundle = [NSBundle mainBundle];
53 NSString *imgUrl = [bundle pathForResource:@"angry_00.jpg" ofType:nil];
54 UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgUrl];
55
56 // 将图片给View
57 imageView.image = img;
58 [self.view addSubview:imageView];
59 self.tom = imageView;
60
61 // 创建点击按钮
62 // 左侧按钮
63 // eat
64 [self createBtn:@"eat" andCGRect:CGRectMake(0, (self.view.bounds.size.height - KBtnSize * KButtonFirst), KBtnSize, KBtnSize)];
65 // cymbal
66 [self createBtn:@"cymbal" andCGRect:CGRectMake(0, (self.view.bounds.size.height - KBtnSize * KButtonSecond), KBtnSize, KBtnSize)];
67 // drink
68 [self createBtn:@"drink" andCGRect:CGRectMake(0, (self.view.bounds.size.height - KBtnSize * KButtonThird), KBtnSize, KBtnSize)];
69 // 右侧按钮
70 // fart
71 [self createBtn:@"fart" andCGRect:CGRectMake((self.view.bounds.size.width - KBtnSize), (self.view.bounds.size.height - KBtnSize * KButtonFirst), KBtnSize, KBtnSize)];
72 // pie
73 [self createBtn:@"pie" andCGRect:CGRectMake((self.view.bounds.size.width - KBtnSize), (self.view.bounds.size.height - KBtnSize * KButtonSecond), KBtnSize, KBtnSize)];
74 // scratch
75 [self createBtn:@"scratch" andCGRect:CGRectMake((self.view.bounds.size.width - KBtnSize), (self.view.bounds.size.height - KBtnSize * KButtonThird), KBtnSize, KBtnSize)];
76
77 // 猫身上的按钮
78 // 头
79 [self createBtn:@"knockout" andCGRect:CGRectMake(62, 80, 190, 162)];
80 // 尾巴
81 [self createBtn:@"angry" andCGRect:CGRectMake(212, 352, 35, 80)];
82 // 左脚
83 [self createBtn:@"footLeft" andCGRect:CGRectMake(158, 430, 45, 32)];
84 // 右脚
85 [self createBtn:@"footRight" andCGRect:CGRectMake(112, 430, 45, 32)];
86
87
88 }
89
90 /** 创建点击按钮 */
91 - (void) createBtn:(NSString *)btnTitle andCGRect:(CGRect)frame
92 {
93 UIButton *btn = [[UIButton alloc] initWithFrame:frame];
94 [btn setTitle:btnTitle forState:UIControlStateNormal];
95 [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
96
97 // 设置按钮背景图片
98 UIImage *imgs = [UIImage imageNamed:btnTitle];
99 if (imgs) {
100 [btn setBackgroundImage:imgs forState:UIControlStateNormal];
101 } else {
102 }
103
104
105 // 监听点击事件
106 [btn addTarget:self action:@selector(playAnimation:) forControlEvents:UIControlEventTouchUpInside];
107
108 [self.view addSubview:btn];
109 }
110
111 /** 点击事件 */
112 - (void)playAnimation : (UIButton *)button
113 {
114 if( self.tom.isAnimating) return;
115 // 获取点击按钮的名称
116 NSString *str = [button titleForState:UIControlStateNormal];
117
118 // 根据点击按钮的名称从plist文件中获取图片数量
119 int count = [self.dict[str] intValue];
120
121 // 创建图片数组
122 NSMutableArray *imageArray = [NSMutableArray array];
123
124 // 循环获取所以图片
125 for (int i = 0; i < count; i++) {
126 // 图片名称
127 NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", str, i];
128 // 图片绝对路径
129 NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
130 // 获取图片
131 UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];
132 // 将图片存入图片数组
133 [imageArray addObject:img];
134
135 }
136
137 // 创建图片动画
138 self.tom.animationImages = imageArray;
139 // 每0.075秒播一张图片
140 self.tom.animationDuration = count * 0.075;
141 // 只播一次
142 self.tom.animationRepeatCount = 1;
143 // 开始动画
144 [self.tom startAnimating];
145 // 动画结束后销毁图片数组
146 [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
147 }
148
149
150 - (void)didReceiveMemoryWarning
151 {
152 [super didReceiveMemoryWarning];
153 // Dispose of any resources that can be recreated.
154 }
155
156 @end
时间: 2024-10-23 10:06:57