找色块--小游戏

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

 

posted @ 2015-12-29 13:29  My_blogs龙  阅读(535)  评论(0编辑  收藏  举报