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.效果

IOS开发小项目—找色块游戏的更多相关文章

  1. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  2. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  3. 李洪强iOS开发之 - 项目架构

    李洪强iOS开发之 - 项目架构 01 - 在Appdelegate中设置跟控制器 //导入头文件

  4. iOS开发-项目的完整重命名方法,图文教程。

    前言:在IOS开发中,有时候想改一下项目的名字,都会遇到很多麻烦.直接改项目名吧,XCODE又不会帮你改所有的名字.总是有很多文件.文件夹或者是项目设置的项.而且都是不能随便改的,有时候改着改着,编译 ...

  5. iOS开发--完整项目

    完整项目 Phonetic Swift 写的一个 iOS 版的 Phonetic Contacts,功能很多,其中昵称功能非常实用,已在 GitHub 开源并上架 App Store v2ex – v ...

  6. iOS开发小技巧 -- tableView-section圆角边框解决方案

    [iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...

  7. ios开发小技巧之提示音播放与震动

    在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...

  8. iOS学习——iOS开发小知识点集合

    在iOS学习和开发过程中,经常会遇到一些很小的知识点和问题,一两句话就可以解释清楚了,这样的知识点写一篇随笔又没有必要,但是又想mark一下,以备不时之需,所以就有了本文.后面遇到一些小的知识点会不断 ...

  9. 在IOS开发中,项目的目录结构如何搭建?

    网上有很多关于IOS开发的学习资料.然而却很少有关于设计一个项目时,如何设计其目录结构?这对于自学IOS的程序猿们,无疑有诸多不利.接下来,我就简单的谈下真正在公司中,项目的目录结构如何搭建: 以上为 ...

随机推荐

  1. ssh 无密码登录

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  2. ORA-12545: 因目标主机或对象不存在, 连接失败

    ORA-12545: 因目标主机或对象不存在, 连接失败 1. 问题描述 XP系统下同时安装了AX1应用程序和升级版AX2,连接同一个在本机Oracle客户端上配置的连接实例,其中AX2显示链接成功, ...

  3. Python的类实例属性访问规则

    一般来说,在Python中,类实例属性的访问规则算是比较直观的. 但是,仍然存在一些不是很直观的地方,特别是对C++和Java程序员来说,更是如此. 在这里,我们需要明白以下几个地方: 1.Pytho ...

  4. 单页web应用(SPA)的简单介绍

    单页 Web 应用 (single-page application 简称为 SPA) 是一种特殊的 Web 应用.它将所有的活动局限于一个Web页面中,仅在该Web页面初始化时加载相应的HTML.J ...

  5. 使用isInEditMode解决可视化编辑器无法识别自定义控件的问题

    如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码, 会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to s ...

  6. window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马

    网站安全中,对目录的执行权限是非常敏感的,一般来说,可以写入的目录是不能够拥有脚本的执行权限的,可写入的目录如: data.uploads,data目录主要是基本配置文件和缓存数据,uploads则是 ...

  7. hdu4639 hehe ——斐波纳契数列,找规律

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4639 refer to: http://blog.csdn.net/dongdongzhang_/ar ...

  8. PHP部分---字符串处理(文中例子不断更新);

    字符串处理: 1.去除   ltrim(),rtrim()函数  trim()函数 trim(字符串名)函数:--去掉字符串两边的空格 ltrim(字符串名,“特殊字符”);--去除字符串左边的空格和 ...

  9. Android——模拟文件拷贝

    模拟文件拷贝:要求:要用progressDialog和子线程来模拟显示拷贝进度:进度完成后在主界面提示拷贝完成,分别使用普通方式和消息机制编写. layout文件: <?xml version= ...

  10. JS中的call()方法的理解

    fn.call(obj,arg1,arg2);这是call()方法的使用形式,apply()是差不多的.作用是用obj对象来替换fn中的this 举个栗子: function A(){ this.co ...