标签:
//打地鼠核心代码:
1 #import "ViewController.h"
2 #define Krandom arc4random()%(115-100+1)+100
3 @interface ViewController ()
4 {
5 NSInteger _n; //记录当前的Krandom值
6 CGFloat _grade;//记录点击中的次数
7 NSInteger _counter;//记录循环次数
8 }
9
10
11 @property(nonatomic, retain)UIButton *but;
12 @property(nonatomic, retain)UIButton *but1;
13 @property(nonatomic, retain)NSTimer *timer;
14 @property(nonatomic, retain)UILabel *textCJ;
15
16
17 @end
18
19 @implementation ViewController
20
21 - (void)viewDidLoad {
22 [super viewDidLoad];
23 _grade = 0;
24 _counter = 0;
25 [self loadButton];
26 [self star];
27 [self stop];
28
29 }
30
31 //第一步 完成基本布局(位置大小背景色,及相应点击事件)--------------------------------------
32 //地鼠button布局 和 分数显示初始化
33 -(void)loadButton
34 {
35 NSInteger n = 100;
36 //用循环方式创建button按钮 4*4方格
37 for (int i = 0 ; i < 4; i++) {
38 for (int j = 0 ; j <4 ; j++ ) {
39 //设置按钮类型
40 _but = [UIButton buttonWithType:UIButtonTypeCustom];
41 //用循环动态确定按钮位置 前后左右的间距要把握好
42 _but.frame = CGRectMake(i * self.view.frame.size.width/4 +10 , j * self.view.frame.size.width/4 + 20 , self.view.frame.size.width/4 - 20 , 50);
43 //添加按钮
44 [self.view addSubview:_but];
45 //设置按钮背景色
46 [_but setBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
47 //设置按钮tag值
48 [_but setTag:n];
49 n++;
50 }
51 _textCJ = [[UILabel alloc] initWithFrame:CGRectMake(140, 400, 100, 40)];
52 _textCJ.backgroundColor = [UIColor yellowColor];
53
54 [self.view addSubview:_textCJ];
55
56 NSString *str = [[NSString alloc] initWithFormat:@"%2.f",_grade];
57
58 _textCJ.text = str;
59 }
60 }
61
62 //开始按钮 与 计时器timmer初始化
63 -(void)star
64 {
65 _but1 = [[UIButton alloc] initWithFrame:CGRectMake(30, self.view.frame.size.height - 30 , self.view.frame.size.width/2 - 60 , 30)];
66 _but1.backgroundColor = [UIColor greenColor];
67 //添加按钮
68 [self.view addSubview:_but1];
69 [_but1 setTitle:@"开始" forState:UIControlStateNormal];
70 //添加按钮点击事件
71 [_but1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
72 //计时器
73 _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(paint:) userInfo:nil repeats:YES];
74 }
75 //结束按钮
76 -(void)stop
77 {
78 UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
79 but.frame = CGRectMake(self.view.frame.size.width/2 + 30, self.view.frame.size.height - 30, self.view.frame.size.width/2 - 60 , 30);
80 but.backgroundColor = [UIColor redColor];
81 [but setTitle:@"结束" forState:UIControlStateNormal];
82
83 [but addTarget:self action:@selector(ting:) forControlEvents:UIControlEventTouchUpInside];
84 [self.view addSubview:but];
85 }
86
87
88 //第二步,处理按钮点击事件--------------------------------------
89
90 //结束按钮的点击事件(1.按钮背景色设置为最初状态 2.计算成绩 3. )
91 -(void)ting:(UIButton *)butt
92 {
93 [_timer invalidate]; //停止计时器,释放计时器
94
95 //同时通过循环形式,将背景色变为最初状态
96 for (int i = 100 ; i < 116 ; i ++ ) {
97 //用tag值,找button
98 butt = (UIButton *)[self.view viewWithTag:i];
99 [butt setBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
100 }
101
102 //计算出成绩 ,并在label上显示
103 _grade = _grade/50 * 100; //地鼠会出现50次,求出百分制成绩
104 //将NSInteger类型的成绩转换为字符串类型
105 NSString *str = [[NSString alloc] initWithFormat:@"%.2f",_grade];
106 _textCJ.text = str; //将字符串类型的成绩赋给显示label
107
108 _grade = 0; //重置成绩 _grade为点击的次数
109 //成绩重置为0时,重新开始计数
110 _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(paint:) userInfo:nil repeats:YES];
111
112 }
113
114 //触发计时器是执行的事件
115 - (void)paint:(UIButton *)bt//计时器触发事件
116 {
117 for (int i = 100;i < 116;i++)
118 {
119 _but = (UIButton *)[self.view viewWithTag:i];
120 [_but setBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
121 }
122
123 //记录当前tag值 让tag值的出现成为随机事件
124 _n = Krandom;//随机事件宏定义Krandom
125
126 bt = (UIButton *)[self.view viewWithTag:_n];//找出随机tag值的button位置
127
128 //找出随机tag值的button位置后,为其添加新的背景图片
129 [bt setBackgroundImage:[UIImage imageNamed:@"4.jpg"] forState:UIControlStateNormal];
130
131 //按钮点击事件
132 [bt addTarget:self action:@selector(selector:) forControlEvents:UIControlEventTouchUpInside];
133
134 if (_counter >= 50)//每一次触发计数一次,若果触发50次就结束计时器,跳出循环
135 {
136 _counter = 0;//重置计数
137 //并触发停止事件
138 [self ting:bt];
139 }
140
141 _counter ++;//计数累加
142 //NSLog(@"this button tag is %ld",bt.tag);
143 }
144
145
146 //击打地鼠的点击事件
147 - (void)selector:(UIButton *)sender
148 {
149 if (sender.tag == _n)//判断是否所击打位置是当前显示地鼠位置
150 {
151 [sender setBackgroundImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
152
153 _grade++;
154 }
155 }
156
157 //计时器触发事件
158 //开始按钮点击事件,将计时器添加到NSRunloop循环中
159 - (void)click
160 {
161 [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
162 }
大体思路:
1.用循环方式完成基本布局
2.计时器timmer的使用要和按钮状态保持一致
效果图:
标签:
原文地址:http://www.cnblogs.com/erdeng/p/4842288.html