找色块--小游戏
2015-12-29 13:29
381 查看
#import "ViewController.h"
#import "ViewController.h" #define SCREE_WITDH CGRectGetWidth([UIScreen mainScreen].bounds) #define SCREE_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds) @interface ViewController () { NSArray *colorList; int allRow; UIView *backView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self loadData]; [self createView]; } //加载数据 - (void)loadData{ allRow = 2; colorList = @[[UIColor yellowColor],[UIColor cyanColor],[UIColor redColor],[UIColor greenColor],[UIColor grayColor],[UIColor purpleColor],[UIColor blueColor]]; } //创建视图 - (void)createView{ backView = [[UIView alloc]initWithFrame:CGRectMake(0, 120, SCREE_WITDH, SCREE_WITDH)]; backView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:backView]; int space = 10; CGFloat witdh = (SCREE_WITDH-40-(allRow-1)*space)/allRow; int rand = arc4random()%colorList.count; int num = arc4random()%(allRow*allRow)+1; for (int i=0; i<allRow; i++) { for (int j=0; j<allRow; j++) { UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20+(witdh+space)*i, 20+(witdh+space)*j, witdh, witdh)]; view.backgroundColor = colorList[rand]; view.layer.cornerRadius = 15; view.tag = allRow*j+i+1; [backView addSubview:view]; //改变某个色块的透明度(可以在这增加难度) if (num == view.tag) { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action:)]; [view addGestureRecognizer:tap]; view.alpha = 0.5; }else{ view.alpha = 1; } } } } - (void)action:(UITapGestureRecognizer *)sender{ [backView removeFromSuperview]; allRow++; [self createView]; }
相关文章推荐
- window平板的几个快捷操作
- 详细讲述iOS自定义转场
- 异常解决:Error contacting service. It is probably not running.
- [java 编程思想学习笔记] java 线程状态
- zabbix重启命令
- appium 不用每次都安装APP
- android assets
- Android通过代码模拟物理、屏幕点击事件
- 解决ecshop模板兼容jquery问题
- HTTP协议中keep-alive
- Beta版本项目展示要求
- ADB 录制屏幕命令
- HttpClient使用详解
- TCP连接中的TIME_WAI状态
- 5-05. QQ帐户的申请与登陆(25)(map运用)(ZJU_PAT)
- The Java Programming Language4th读书笔记-第十二章 异常与断言
- Java第一课:读输入、变量类型、常量
- Xcode 快捷键
- git的使用
- 关于UIView的autoresizingMask属性的研究