1.项目代码:
@interface NextViewController ()
{
int r ;//色块层数的全局变量
int m;//后面用于tag值的变化
UIView *view;//色块
NSArray *color;//颜色库
UIImageView *baview;//作为色块的地图,也可以用UIView
UILabel *label;
}
@end
@implementation NextViewController
- (void)viewDidLoad {
// 赋初值 建立最开始的模型
[super viewDidLoad];
self.title = @"找色块";
r = 2;//初始化状态为2层
m = 1;
self.view.backgroundColor = [UIColor whiteColor];//初始颜色为白色
[self color];
[self loadData];
[self label];
}
//颜色库
-(void)color{
color = @[[UIColor colorWithRed:0.494 green:0.905 blue:0.493 alpha:1.000],[UIColor greenColor],[UIColor colorWithRed:0.000 green:0.091 blue:0.998 alpha:1.000],[UIColor cyanColor],[UIColor yellowColor],[UIColor purpleColor],[UIColor lightGrayColor],[UIColor darkGrayColor],[UIColor blackColor],[UIColor colorWithRed:0.868 green:0.336 blue:0.760 alpha:1.000],[UIColor magentaColor],[UIColor orangeColor],[UIColor brownColor]];}
//--------------------创建色块-------------------
-(void)loadData{
int index = arc4random()%(r*r)+1;
int ii = arc4random()%color.count;
baview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 120, CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame))];
baview.backgroundColor = [UIColor whiteColor];
// 使用UIImageView必须要使用下面语句可以使用手势
baview.userInteractionEnabled = YES;
[self.view addSubview: baview];
for (int n =0; n<r; n++)
{
for (int i = 0; i<r; i++)
{
CGFloat k = CGRectGetWidth(self.view.frame);
view = [[UIView alloc]initWithFrame:CGRectMake(20+(k-40)/r*i, 20+(k-40)/r*n, (k-60)/r, (k-60)/r)];
view.layer.cornerRadius = 10;//设置拐角
view.layer.masksToBounds = YES;
view.backgroundColor = color[ii];//随机颜色
view.tag = r*i+n+1;
//任意选择一个模块添加点击手势(通过tag值随机选择)
if (index == view.tag)
{
UITapGestureRecognizer *tapge = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tanp:)];
tapge.numberOfTapsRequired = 1;
view.alpha = 0.5;
// 添加手势
[view addGestureRecognizer:tapge];
}
[baview addSubview:view];
}
}
}
-(void)tanp :(UITapGestureRecognizer*)sender{
r++;
m++;
[self loadData];//点击后 重新更新色块
[self text];//最下面文字显示
[self label];//最上端文字显示
}
//文字显示模块,判断达到了多少 出现什么文字
-(void)text{
if (r>5&&r<7) {
UILabel *llabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];
llabel.tag = 100;
llabel.textAlignment = YES;
llabel.text = @"你的视力很好哦!";
[self.view addSubview:llabel];
}
if (r>7&&r<11) {
UILabel *llabel = (UILabel*)[self.view viewWithTag:100];
llabel.hidden = YES;
}
if (r>20&&r<23) {
UILabel *llabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];
llabel.tag = 100;
llabel.textAlignment = YES;
llabel.text = @"你真牛!!!";
[self.view addSubview:llabel];
}
if (r>35&&r<40) {
UILabel *llabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];
llabel.tag = 100;
llabel.textAlignment = YES;
llabel.text = @"大神,顶礼膜拜!!!!!!";
[self.view addSubview:llabel];
}
if (r>40) {
UILabel *llabel = (UILabel*)[self.view viewWithTag:100];
llabel.hidden = YES;
}
}
//数字块,显示到了第几层
-(void)label{
label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2-250, 150, 50)];
label = [UIColor whiteColor];
label.tag = m+500;
label.textAlignment = YES;
NSString *te = [NSString stringWithFormat:@"现在是第%d层",r-1];
label.text = te;
label.textColor = [UIColor blackColor];
[self.view addSubview:label];
[self remove];
}
//移除出现过的视图,以免发生重叠
-(void)remove{
UITextField *textf = (UITextField *)[self.view viewWithTag:label.tag - 1 ];
[textf removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2.效果