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. Python代码规范与命名规则

    1.模块 模块尽量使用小写命名,首字母保持小写,尽量不要用下划线(除非多个单词,且数量不多的情况) # 正确的模块名 import decoder import html_parser # 不推荐的模 ...

  2. 简单介绍tomcat中maxThreads,acceptCount,connectionTimeout

    <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOW ...

  3. 使用R的数据库查询

    JS 很多方法可以用R查询数据.这篇文章展示了三种最常见的方法: 运用 DBI 使用dplyr语法 使用R note book 背景 最近的一些软件包改进可以更轻松地将数据库与R一起使用.下面的查询示 ...

  4. RabbitMQ入门_14_Policies

    参考资料:https://www.rabbitmq.com/parameters.html#policies A. Policies 的用途 RabbitMQ 有很多可选参数(x-arguments) ...

  5. JavaScript权威指南--事件处理

    知识要点 客户端JavaScript程序采用了异步事件驱动变成模型(13.3.2节).这种风格并不只应用于web编程,所有使用图形用户界面的应用程序都采用它,它们静待某些事件发生,然后响应. 事件就是 ...

  6. LeetCode--112--路径总和

    问题描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例:  给定如下二叉树,以及目标和 s ...

  7. Leetcode 74

    class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) ...

  8. HDOJ1005

    #include "iostream" using namespace std; int fun(int A,int B,int n) { ,y = ,z; || n == ) ; ...

  9. Excel 版本对应

    (1) 1985年:Excel 1.0 (2) 1993年:Excel 5.0——Office 4.2 (3) 1995年:Excel 7.0(Excel 95)——Office 95 (4) 199 ...

  10. Ajax中Delete请求参数 后台无法获取的解决方法(Restful风格)

    方法一: 在ajax中写入data来传参时,直接把参数拼接到url后面 例如: $.ajax({ url: '/cyberspace/vrv/event/delete/1002?startTime=& ...