美文网首页
Don't Touch whiteBlock

Don't Touch whiteBlock

作者: 蓝蓝的白云 | 来源:发表于2016-08-14 14:53 被阅读11次
随笔写了一个简单实现别踩白块的小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讨论,相互学习

相关文章

  • Don't Touch whiteBlock

    随笔写了一个简单实现别踩白块的小demo,具体实现思路如下:创建一个6行4列的button界面,每行采用随机数随机...

  • 请勿乱动Please, Don't Touch Anyt

    本文是目前PDTA网上最全亮25盏灯攻略(写这篇文章时只有最多亮22盏灯的攻略,其他的最全攻略应该是以前版本,只有...

  • linux对文件的操作

    1 touch 创建空白文件 -t 指定创建文件的日期,不指定默认当前时间 touch -t 1412281130...

  • 《洛丽塔》:洛丽塔,我的不可触碰

    “Don't touch me, I'll die if you touch me .” 《洛丽塔》 初见时,她趴...

  • Don't see, don't be chea

    叁品姐姐每天分享一段爱情感悟,幸福可以很简单,点击右上角关注哦。 人和人的出场顺序真的很重要 Cold will ...

  • Don't touch

    昨天,有个好朋友给壹壹分享share了个视频video . 壹壹,作为一个好奇curiosity宝宝,直接马上地d...

  • Don't take it personal.

    Don't touch. There is no me or mine. To Zeray

  • SNH48金曲大赏汇报MV《Don't Touch》、《

    伴随着SNH48 GROUP第4届年度金曲大赏的启动,第3届金曲大赏两支夺冠曲目《Don't Touch》、《我的...

  • docker 笔记6

    Dockerfile 定制镜像 touch Dockerfile docker build -t myngin...

  • Parameter Settings

    What’s the difference for setting don’t touch on cell, pi...

网友评论

      本文标题:Don't Touch whiteBlock

      本文链接:https://www.haomeiwen.com/subject/czmysttx.html