找色块--小游戏

<span style="font-size:18px;">#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];
    
    
}
</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值