IOS开发小项目—找色块游戏
2015-07-27 21:49
447 查看
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.效果
@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中NSUserDefaults的用法(轻量级本地数据存储) (转)
- iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位
- iOS反射机制
- iOS单例模式(Singleton)
- Xcode 7如何 免费 真机调试iOS应用
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
- iOS设计模式 - 桥接
- iOS陀螺仪计算行走的步数
- iOS开发---轮播图模块(普通版)
- iOS开发之*.a静态库注意事项
- IOS的safari浏览器的电话号码识别功能的禁用
- js判断请求对象是IOS还是安卓用户
- iOS 半透明新手引导 手把手教你做
- iOS tableView的刷新 笔记
- iOS OC08,09_内存管理
- iOS学习笔记--(c基础题9)
- ios申请真机调试(Xcode 5)详细解析
- iOS 创建静态库static library
- IOS中的block的retain循环
- iOS-WebP