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. 【NOI2013】小Q的修炼

    题目链接:http://uoj.ac/problem/123 又开提答坑啦,要不是一定要讲题谁他妈要这样伤害自己 CASE 1,2 首先可以打一个通用暴力,用于模拟操作过程,对于每一个操作随机一个选择 ...

  2. hdu 5144 NPY and shot 物理+三分

    NPY and shot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pro ...

  3. django模型的元数据Meta

    模型的元数据,指的是“除了字段外的所有内容”,例如排序方式.数据库表名.人类可读的单数或者复数名等等.所有的这些都是非必须的,甚至元数据本身对模型也是非必须的.但是,我要说但是,有些元数据选项能给予你 ...

  4. [IOS][sqlite][SQL][数据库]SQL基本语句大全

    参考:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html 一.基础 1.说明:创建数据库CREATE DATABASE d ...

  5. angular5中使用echart的方法

    注意两点安装的版本 安装好后可以参照echart的官网使用 1.实现package.json中安装这两个包 2.index.html中引入 3.在appModule中添加 然后再html中就可以这么使 ...

  6. Solaris 11, gcc 的安装

    注意点在于, 头文件在另外一个包system/header里,需要另外安装 pkg pkg install system/header

  7. 20170228VBA提取邮件部分信息

    Sub 获取OutLook收件箱主题和正文() On Error Resume Next Dim sht As Worksheet Dim olApp As Outlook.Application D ...

  8. linux中tomcat内存溢出PermGen space

    1.若是部署时候,一个tomcat下面项目越少越好,单独为一个项目配置tomcat(在客户给你充足的端口的情况下) 2.在维护的时候,若一个tomcat下放多个项目的话,这时候可以把所有jar包放在t ...

  9. linux--多进程进行文件拷贝

    学习IO的时候,我们都曾经利用文件IO函数,标准IO函数都实现了对文件的拷贝, 对某一个文件进行拷贝时,我们可以考虑一下几种方式: a.单进程拷贝: 假设某一文件需要拷贝100字节,每一个时间片可以完 ...

  10. iOS UI-(多)视图控制器的生命周期、加载方法和模态视图方法以及屌丝方法

    #import "ViewController.h" #import "SecondViewController.h" @interface ViewContr ...