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