1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态

首先想到的是扩展UIColor

首先在.h中声明

@interface UIColor (Color)
+(UIColor*)LightOnColor;
+(UIColor*)LightOutColor;
@end

在.m中实现如下:

效果图如下:

@implementation UIColor (Color)//UIColor类的扩展,就是Category
+(UIColor*)LightOnColor//设置开灯方法,返回开灯时的按钮颜色
{
    return [UIColor colorWithRed:139/255.0 green:254/255.0 blue:118/255.0 alpha:1.0f];
}
+(UIColor*)LightOutColor//设置关灯方法,返回关灯时的按钮颜色
{
    return [UIColor colorWithRed:176/255.0 green:176/255.0 blue:176/255.0 alpha:1.0f];
}
@end

在CHViewController.m中的viewDidLoad中实现

#import "CHViewController.h"
#import "UIColor+Color.h"//导入头文件
@interface CHViewController ()//extension扩展
@property (nonatomic ,retain)NSMutableArray *mutableArray;//定义可变数组,存放25个按钮对象
@property NSInteger lightCount;//灯的点亮数
@property (nonatomic,retain)UIButton * btnHead;//标题按钮
@end

#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]//生成rgb颜色对象,定义宏来实现颜色的设置

- (void)viewDidLoad
{
    _lightCount=0;//记录开灯个数
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];//设置父视图,在rootView上的那个视图
    view.backgroundColor=[UIColor blackColor];//父视图颜色设置为黑色,为了在视图周围显示黑边效果
    [self.view addSubview:view];//把父视图贴到Root视图上面
     _mutableArray=[NSMutableArray array];//定义可变数组来存放25给button实例对象
     _btnHead=[[UIButton alloc]initWithFrame:CGRectMake(1, 1, 318, 50)];//这个按钮就是最上面的蓝色菜单按钮,显示亮灯个数和总数
    _btnHead.backgroundColor=RGB(100, 152, 255);//[UIColor colorWithRed:100/255.0 green:152/255.0 blue:255/255.0 alpha:1];
    //设置标题字体
    UIFont *titleFont=[UIFont boldSystemFontOfSize:50];//设置字体大小
     _btnHead.titleLabel.font=titleFont;//设置按钮字体
    //设置标题按钮不接受用户响应
     _btnHead.userInteractionEnabled=NO;
    
     [_btnHead setTitle:@"0/25" forState:UIControlStateNormal];//按钮上的标题
     [_btnHead setTitleColor:[UIColor orangeColor] forState: UIControlStateNormal];//标题按钮颜色设置
    UIView *viewBottom=[[UIView alloc]initWithFrame:CGRectMake(1, 52, 318, 430)];//创建标题按钮下面的视图
    viewBottom.backgroundColor=RGB(255,216,145);//[UIColor colorWithRed:255/255.0 green:216/255.0 blue:145/255.0 alpha:1.0];//设置背景颜色
     [self.view addSubview:_btnHead];//把蓝色按钮放到root根视图里面
    [self.view addSubview:viewBottom];//把蓝色按钮下面的视图放到root根视图里面
   
     for (int i=0; i<25; i++) {//25次循环,创建25个黑色视图作为button的黑边并创建25给button对象放到数组里
        UIView *viewBack=[[UIView alloc]initWithFrame:CGRectMake(i%5*61.6+10,i/5*61.6+10, 51.6, 51.6)];//黑色view
          viewBack.backgroundColor=[UIColor blackColor];
        UIButton * button=[UIButton buttonWithType:UIButtonTypeRoundedRect];//创建按钮对象
        button.frame=CGRectMake( i%5*61.6+11,i/5*61.6+11, 49.6, 49.6);/////////设置按钮颜色
        button.backgroundColor=[UIColor LightOutColor];
        [button addTarget:self action:@selector(OnClick:) forControlEvents:UIControlEventTouchUpInside];//按钮事件
        [viewBottom addSubview:viewBack];//root视图上添加黑色view
       [viewBottom addSubview:button];//添加按钮
       [_mutableArray addObject:button];//按钮加入动态数组中
    }
    
    UIView *resetView=[[UIView alloc]initWithFrame:CGRectMake(109, 350, 100, 40)];//重置按钮后面的黑色视图,为了给按钮加黑色框
    resetView.backgroundColor=[UIColor blackColor];
    [viewBottom addSubview:resetView];//把视图放到下面的父视图里
    UIButton * resetButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];//声明重置按钮
    resetButton.frame=CGRectMake( 110,351, 98, 38);
    resetButton.backgroundColor=[UIColor whiteColor];
    
    [resetButton setTitle:@"Reset" forState:UIControlStateNormal];
    [resetButton addTarget:self action:@selector(OnClickReset) forControlEvents:UIControlEventTouchUpInside];//按钮事件
    [viewBottom addSubview:resetButton];
    resetButton.titleLabel.font=[UIFont systemFontOfSize:30];
    [resetButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//重置按钮事件
-(void)OnClickReset
{
     [_btnHead setTitle:@"0/25" forState:UIControlStateNormal];
    _lightCount=0;//重置状态的时候,灯数归零
     for (int i=0; i<25; i++) {
     ((UIButton *)[_mutableArray objectAtIndex:i]).backgroundColor=[UIColor LightOutColor];//设置所有的按钮颜色为关灯状态
     }
    
}
//对颜色状态取反
-(void)negation:(int)i
{
    if ([((UIButton *)[_mutableArray objectAtIndex:i]).backgroundColor isEqual:[UIColor LightOutColor]]) {
        ((UIButton *)[_mutableArray objectAtIndex:i]).backgroundColor=[UIColor LightOnColor];
        _lightCount++;//如果被点亮就个数加一
    }
    else {
        ((UIButton *)[_mutableArray objectAtIndex:i]).backgroundColor=[UIColor LightOutColor];
        _lightCount--;//如果被关闭就个数减一
    }
}

-(void)OnClick:(UIButton *)button
{
    int i= [_mutableArray indexOfObject:button];
    //被点击的按钮颜色取反
    [self negation:i];
    //如果不是每列第一个
     if (i%5-1>=0) {
         [self negation:i-1];
    }
     //如果不是每行第一个
    if (i%5!=4) {
        [self negation:i+1];
    }
    if (i>=5) {
         [self negation:i-5];
    }
        if (i<20) {
         [self negation:i+5];
    }
    if (_lightCount==0) {//灯全部关闭,通关
              UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"恭喜" message:@"通关了!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    NSString *str=[NSString stringWithFormat:@"%i/25",_lightCount];
    [_btnHead setTitle:str forState:UIControlStateNormal];

}

@end

IOS 作业项目(1) 关灯游戏 (百行代码搞定)的更多相关文章

  1. 30行代码搞定WCF并发性能测试

    [以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main()         {               List< ...

  2. 10行代码搞定移动web端自定义tap事件

    发发牢骚 移动web端里摸爬滚打这么久踩了不少坑,有一定移动web端经验的同学一定被click困扰过.我也不列外.一路走来被虐的不行,fastclick.touchend.iscroll什么的都用过, ...

  3. [Unity Editor]10行代码搞定Hierarchy排序

    在日常的工作和研究中,当给我们的场景摆放过多的物件的时候,Hierarchy面板就会变得杂乱不堪.比如这样:    过多的层次结构充斥在里面,根层的物件毫无序列可言,整个层次面板显示非常的杂乱不堪,如 ...

  4. 100行代码搞定抖音短视频App,终于可以和美女合唱了。

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由视频咖 发表于云+社区专栏 本文作者,shengcui,腾讯云高级开发工程师,负责移动客户端开发 最近抖音最近又带了一波合唱的节奏,老 ...

  5. 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学

    编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...

  6. 7行代码搞定WEB服务

    作为一个 Java 程序猿,写代码久了,各种技术也就都尝试了一个遍. 先从 SSH1(Spring.Struts1.Hibernate)摸爬滚打转变到 SSH2(Spring.Struts2.Hibe ...

  7. 当小程序遇见物联网IoT,几行代码搞定智能插座控制

    在 5G 热潮的推动下,与其紧密结合的物联网(IoT)正日益成为个人和企业工作生活中的重要组成部分,它为企业和个人带来了操作流程的改进和更好的生活体验,随着人工智能(AI)技术的日趋成熟,IoT 与 ...

  8. BaseHttpListActivity,几行代码搞定Android Http列表请求、加载和缓存

    Android开发中,向服务器请求一个列表并显示是非常常见的需求,但实现起来比较麻烦,代码繁杂. 随着应用的更新迭代,这种需求越来越多,我渐渐发现了实现这种需求的代码的共同点. 于是我将Activit ...

  9. python爬煎蛋妹子图--20多行代码搞定煎蛋妹子图库

    如果说一个人够无聊的话... 就会做一些十分美(wei)丽(suo)的事情啦哈哈哈... 好的,话不多说,进入正题. 正如标题所示,我们今天的目标很简单: 代码要少,妹子要好. 步骤如下: 1. 首先 ...

随机推荐

  1. 磁条卡,IC卡,ID卡,信用卡芯片卡,信用卡磁条卡 等等的区别

    1.条码卡:该卡卡面上有一串条码,通过扫描枪或者相应的条码读卡器读出该条码卡的卡号.根据条码的不同又分为39码等其它码.条码卡仅仅是一个编号,不存蓄其它内容.特点:价格便宜类似磁卡. 2.磁条卡:类似 ...

  2. jstl中<c:forEach>的用法

    在JSP的开发中,迭代是经常要使用到的操作.例如,逐行的显示查询的结果等.在早期的JSP中,通常使用Scriptlets来实现Iterator或者Enumeration对象的迭代输出.现在,通过JST ...

  3. Qt5_各种路径

    1.Qt5.3.2 -- vs2010 -- OpenGL 1.1.发布时需要的 DLL文件的路径 F:\ZC_software_installDir\Qt5.3.2_vs2010\5.3\msvc2 ...

  4. 《剑指offer》第三十九题(数组中出现次数超过一半的数字)

    // 面试题39:数组中出现次数超过一半的数字 // 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例 // 如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, ...

  5. SSD论文理解

    SSD论文贡献: 1. 引入了一种单阶段的检测器,比以前的算法YOLO更准更快,并没有使用RPN和Pooling操作: 2. 使用一个小的卷积滤波器应用在不同的feature map层从而预测BB的类 ...

  6. 【Jmeter】Linux(Mac)上使用最新版本Jmeter(5.0)做性能测试

    本文我们一起来学习在Linux(Mac)上利用Jmeter进行性能测试并生成测试报告的方法. 环境准备 JDK 访问这个地址 [JDK11.01],根据实际环境下载一个JDK. Jmeter Jmet ...

  7. 为什么HikariCP被号称为性能最好的Java数据库连接池,如何配置使用

    转自:https://blog.csdn.net/clementad/article/details/46928621 HiKariCP是数据库连接池的一个后起之秀,号称性能最好,可以完美地PK掉其他 ...

  8. [.NET开发] NPOI导出

    //导出全部 expertPara = GetExpetPara(); expertPara.BeginIndex = pager.CurrentPageIndex; expertPara.EndIn ...

  9. Linux : 密码正确不能正常登陆,日志提示Could not get shadow information for user

    今天,再玩Centos7的时候,尝试修改了下ssh的端口.因为默认开启了SELinux,如果没有修改这个文件配置就修改端口sshd服务就不能正常启动了. 但是,当我修改会22端口的时候还是不能正常登陆 ...

  10. 开发shellcode的艺术

    专业术语 ShellCode:实际是一段代码(也可以是填充数据) exploit:攻击通过ShellCode等方法攻击漏洞 栈帧移位与jmp esp 一般情况下,ESP寄存器中的地址总是指向系统栈且不 ...