Don't Touch whiteBlock

随笔写了一个简单实现别踩白块的小demo,具体实现思路如下:创建一个6行4列的button界面,每行采用随机数随机一个button,设置此button的背景颜色为黑色,其他button背景均为黑色。当点击白色button后,弹框显示点击错误,界面重新归0,当点击黑色button后,上一行的黑色button的背景颜色延续到下一行,然后第一行随机一个button的颜色为黑色,其余为白色。实现如下:

1. 创建button按钮

- (void)setupButton
{ 
    for (int i = 0; i<6; i++) {
         NSInteger black =kScreenRandom;
        for (int j = 0; j<4; j++) {
            self.button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            _button.frame = CGRectMake(kScreenWidth/4*j, kScreeHeight/6*i, kScreenWidth/4, kScreeHeight/6);
            _button.layer.borderWidth = 1;
            [_button setBackgroundColor:[UIColor whiteColor]];
            _button.layer.borderColor= [[UIColor blackColor]CGColor];
            _button.tag = 4*i+j+1;
            [self.view addSubview:_button];
            if (j==black) {
                [_button setBackgroundColor:[UIColor blackColor]];
            }
            [_button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }
    }
}
// button 点击事件
- (void)buttonAction:(UIButton *)button
{
    if ([button.backgroundColor  isEqual:[UIColor blackColor]]) {
        for (  int i = 24; i>=5; i--) {
            UIButton *button1 = [self.view viewWithTag:i];
            UIButton *button2 = [self.view viewWithTag:i-4];
    // 将上一排button的颜色传递给下一排
            button1.backgroundColor = button2.backgroundColor;
        }
    }else{
        [self whiteAction];
    }
   // 随机将第一排的某一个button设置为黑色     
    int a =kScreenRandom+1;
    for ( int i =1 ; i<5; i++) {
         if(i==a)
         {
            [[self.view viewWithTag:i] setBackgroundColor:[UIColor blackColor]];
         }
        else
        {
          [[self.view viewWithTag:i] setBackgroundColor:[UIColor whiteColor]];
        }
    }
    _count++;
}

2.弹出框

- (void)whiteAction
{
    NSString *string = [NSString stringWithFormat:@"得分:%ld",_count];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:string message:@"闯关失败" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"重新开始" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        // 重新开始
        [self setupButtonRestart];
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    _count=0;
}

3.重新开始

- (void)setupButtonRestart
{
    for (int i = 0; i<24; i++) {
        [self.view viewWithTag:i+1].backgroundColor = [UIColor whiteColor];
    }
    for (int j = 0; j<6; j++) {
        NSInteger a = arc4random()%3;
    //通过tag找到对应button,设置背景颜色
    [self.view viewWithTag:4*j+a+1].backgroundColor = [UIColor blackColor];
    }  
}
欢迎加群192699811讨论,相互学习
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,554评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,593评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 抱怨挺好的,可以释放自己的情绪!但是不能解决任何问题! 真的自己静下来,问自己的内心,为什么抱怨?可能自己再静的心...
    火火烽阅读 328评论 0 1
  • 文/刘彩霞 (二十三) 从医院出来,已经傍晚时分。我一个人走在这条空荡荡的路上,泪水冻凉了我的...
    彩霞漫天阅读 205评论 0 0