码迷,mamicode.com
首页 > 其他好文 > 详细

找色块--小游戏

时间:2015-12-29 14:20:10      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

#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];
    
    
}

 

找色块--小游戏

标签:

原文地址:http://www.cnblogs.com/ios-wanglong/p/5085433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!