标签:
@interface NextViewController ()
{
int r ;
int m;
int nn;
UIView *view;
NSArray *color;
NSMutableArray *arry;
UIImageView *baview;
UILabel *textfiled;
}
@end
@implementation NextViewController
- (void)viewDidLoad {
// 赋初值 建立最开始的模型
[super viewDidLoad];
self.title = @"找色块";
r = 2;
m = 1;
self.view.backgroundColor = [UIColor whiteColor];
[self color];
[self loadData];
[self textfiled];
}
//颜色库
-(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)-40-(r-1)*5)/r;
CGFloat k = CGRectGetWidth(self.view.frame);
// view = [[UIView alloc]initWithFrame:CGRectMake(20+(k+5)*n, 20+(k+5)*i, k-2, k-2)];
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;
//任意选择一个模块添加手势
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 textfiled];//最上端文字显示
}
//文字显示模块
-(void)text{
if (r>5&&r<7) {
UILabel *textfile = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];
textfile.tag = 100;
textfile.textAlignment = YES;
textfile.text = @"你的视力很好哦!";
[self.view addSubview:textfile];
}
if (r>7&&r<11) {
UILabel *textfile = (UILabel*)[self.view viewWithTag:100];
textfile.hidden = YES;
}
if (r>20&&r<23) {
UILabel *textfile = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];
textfile.tag = 100;
textfile.textAlignment = YES;
textfile.text = @"你真牛!!!";
[self.view addSubview:textfile];
}
if (r>35&&r<40) {
UILabel *textfile = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2+150, 150, 50)];
textfile.tag = 100;
textfile.textAlignment = YES;
textfile.text = @"大神,顶礼膜拜!!!!!!";
[self.view addSubview:textfile];
}
if (r>40) {
UILabel *textfile = (UILabel*)[self.view viewWithTag:100];
textfile.hidden = YES;
}
}
//数字块
-(void)textfiled{
textfiled = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-150-100, self.view.frame.size.height/2-250, 150, 50)];
textfiled.backgroundColor = [UIColor whiteColor];
textfiled.tag = m+500;
arry = [NSMutableArray array];
[arry addObject:@(textfiled.tag)];
textfiled.textAlignment = YES;
NSString *te = [NSString stringWithFormat:@"现在是第%d层",r-1];
textfiled.text = te;
textfiled.textColor = [UIColor blackColor];
[self.view addSubview:textfiled];
// if (textfiled.tag+1 == m+300) {
// textfiled.hidden = YES;
// }
[self remove];
}
//移除出现过的视图,以免发生重叠
-(void)remove{
UITextField *textf = (UITextField *)[self.view viewWithTag:textfiled.tag - 1 ];
[textf removeFromSuperview];
// textf.hidden = YES;
}
//
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
标签:
原文地址:http://www.cnblogs.com/chenhongios/p/4681343.html