效果图如下:
核心代码如下:
TwoViewController.m中
<span style="font-size:14px;color:#666666;">#import "TwoViewController.h"
#import "ThereViewController.h"
@interface TwoViewController ()
{
NSMutableArray *colorlist;
int a;
UILabel *label;
UILabel *label1;
int showTime;
NSTimer *timer;
NSTimer *timer1;
int d;
}
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
colorlist = [NSMutableArray arrayWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor brownColor], nil];
a = 2;
[self loadAll];
}
- (void)loadAll
{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(add) userInfo:nil repeats:YES];
timer.fireDate = [NSDate distantFuture];
timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addd) userInfo:nil repeats:YES];
timer1.fireDate = [NSDate distantFuture];
float w = CGRectGetWidth([UIScreen mainScreen].bounds);
UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
bgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bgView];
label = [[UILabel alloc]initWithFrame:CGRectMake(50, 75, 100, 35)];
label.backgroundColor = [UIColor greenColor];
label.text = [@(showTime)stringValue];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 75, 100, 35)];
label1.backgroundColor = [UIColor greenColor];
// label1.text = [@(c)stringValue];
label1.text = [NSString stringWithFormat:@"第%@层",[@(d)stringValue]];
label1.textColor = [UIColor blackColor];
label1.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(137.5, 600, 100, 35) ;
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"是否交卷?" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
[self.view addSubview:button];
[button addTarget:self action:@selector(xiayiye) forControlEvents:UIControlEventTouchUpInside];
int c = arc4random()%colorlist.count;
for (int i=0; i<a; i++) {
for (int j= 0; j<a; j++) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20+(w-40)/a*j, 200+(w-40)/a*i, (w-40-20)/a, (w-40-20)/a)];
// 设置拐角
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
view.backgroundColor = colorlist[c];
[self.view addSubview:view];
}
}
int x = arc4random()%a;
int y = arc4random()%a;
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(20+(w-40)/a*y, 200+(w-40)/a*x, (w-40-20)/a, (w-40-20)/a)];
v.layer.cornerRadius = 10;
v.layer.masksToBounds = YES;
int z = arc4random()%colorlist.count;
if (z != c) {
v.backgroundColor = colorlist[z];
}else{
v.backgroundColor = [UIColor blackColor];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(next:)];
tap.numberOfTapsRequired = 1;
[v addGestureRecognizer:tap];
[self.view addSubview:v];
}
- (void)xiayiye
{
ThereViewController *there = [[ThereViewController alloc]init];
there.time = [[@(showTime)stringValue]stringByAppendingString:@" S"];
[self.navigationController pushViewController:there animated:YES];
[self stop];
}
- (void)next:(UITapGestureRecognizer *)sender
{
a++;
[self addd];
[self loadAll];
}
- (void)beigin
{
timer.fireDate = [NSDate distantPast];
}
- (void)stop
{
timer.fireDate = [NSDate distantFuture];
}
- (void)add
{
showTime++;
label.text = [@(showTime)stringValue];
}
- (void)addd
{
d++;
label1.text = [NSString stringWithFormat:@"第%@层",[@(d)stringValue]];
}
- (void)viewDidAppear:(BOOL)animated
{
[self beigin];
}
- (void)viewDidDisappear:(BOOL)animated
{
showTime = 0;
[self stop];
}</span>
ThereViewController ,也就是最后一个页面,里面主要传值的问题和重新开始的代码,如下
ThereViewController.h中 ,定义了一个属性
<span style="color:#666666;">#import <UIKit/UIKit.h>
@interface ThereViewController : UIViewController
@property (nonatomic,copy) NSString *time;
@end</span>
ThereViewController.m中
<span style="font-size:14px;color:#666666;">#import "ThereViewController.h"
@interface ThereViewController ()
{
UILabel *labelShouTime;
}
@end
@implementation ThereViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
left.tintColor = [UIColor greenColor];
self.navigationItem.leftBarButtonItem = left;
labelShouTime = [[UILabel alloc]initWithFrame:CGRectMake(137.5, 200, 100, 35)];
labelShouTime.backgroundColor = [UIColor greenColor];
labelShouTime.text = _time;
labelShouTime.textColor = [UIColor redColor];
labelShouTime.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:labelShouTime];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 35)];
label.backgroundColor = [UIColor greenColor];
label.center = self.view.center;
label.text = @"恭喜过关";
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor whiteColor];
button.frame = CGRectMake(140, 100, 100, 35);
[button setTitle:@"再来一次?" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(qiehuan:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)qiehuan:(UIButton *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)back
{
[self.navigationController popToRootViewControllerAnimated:YES];
}</span><span style="font-size:18px;color: rgb(204, 153, 51);">
</span>