UI拓展练习2----关灯游戏

大致思路:1.首先通过循环的方法完成基本布局

2.每一个点都要保证相邻点的状态与它保持一致,(边的位置特殊考虑即可)

  //关灯游戏   1 #import "ViewController.h"
  2
  3 @interface ViewController ()
  4
  5 @property (nonatomic, strong)UIButton *but;
  6 @property (nonatomic, strong)NSString *str;//用来记录灯的当前状态
  7
  8 @end
  9
 10 @implementation ViewController
 11
 12 - (void)viewDidLoad {
 13     [super viewDidLoad];
 14
 15       [self loadButton];//灯布局
 16
 17
 18 }
 19
 20 //基本布局
 21 - (void)loadButton
 22 {
 23     //设置灯的初始状态为亮的状态
 24     _str = [NSString stringWithFormat:@"iconfont-bulb"];
 25     //用循环形式实现基本布局
 26     NSInteger n = 100;
 27     for (int i = 0 ;i < 4 ; i++)
 28         for (int y = 0; y < 4; y++)
 29         {
 30             _but = [UIButton buttonWithType:UIButtonTypeCustom];
 31             //设置位置和大小
 32             _but.frame = CGRectMake(i*self.view.frame.size.width/4+10, y*self.view.frame.size.width/4+80,self.view.frame.size.width/4-20, 50);
 33             //添加
 34             [self.view addSubview:_but];
 35             //设置背景色
 36             [_but setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 37             //设置按钮addtarget/action事件
 38             [_but addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];//点击事件
 39             //设置tag值,区分button
 40             [_but setTag:n];
 41
 42             n++;
 43         }
 44 }
 45
 46 //button的按钮点击触发事件
 47 - (void)Click:(UIButton *)butt
 48 {
 49     NSInteger n = butt.tag;
 50     NSLog(@"%ld",n);
 51     //如果不是亮灯状态
 52     if ([_str isEqual:@"iconfont-4-2"] == NO)
 53     {
 54         //背景色设置成亮灯
 55         [butt setBackgroundImage:[UIImage imageNamed:@"iconfont-4-2"] forState:UIControlStateNormal];
 56         _str = @"iconfont-4-2";
 57     }
 58     else  //否则设置成关灯状态
 59     {
 60         NSLog(@"%@",_str);
 61         _str = @"iconfont-bulb";
 62         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 63     }
 64
 65     //如果为第一行则左右下状态要与其保持一致
 66     if(n == 104 || n == 108 || n == 112)
 67     {
 68         //按钮状态保持一致
 69         butt = (UIButton *)[self.view viewWithTag:n + 1];
 70
 71         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 72
 73         butt = (UIButton *)[self.view viewWithTag:n + 4];
 74
 75         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 76
 77         butt = (UIButton *)[self.view viewWithTag:n - 4];
 78
 79         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 80     }
 81
 82     //如果为最后一行,左右上要与其状态保持一致
 83     else if (n == 103 || n == 107 || n == 111)
 84     {
 85         //按钮状态保持一致
 86         butt = (UIButton *)[self.view viewWithTag:n - 1];
 87
 88         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 89
 90         butt = (UIButton *)[self.view viewWithTag:n + 4];
 91
 92         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 93
 94         butt = (UIButton *)[self.view viewWithTag:n - 4];
 95
 96         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
 97     }
 98     //剩余地方则前后左右都与其状态保持一致
 99     else
100     {
101         butt = (UIButton *)[self.view viewWithTag:n + 1];
102
103         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
104
105         butt = (UIButton *)[self.view viewWithTag:n - 1];
106
107         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
108
109         butt = (UIButton *)[self.view viewWithTag:n + 4];
110
111         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
112
113         butt = (UIButton *)[self.view viewWithTag:n - 4];
114
115         [butt setBackgroundImage:[UIImage imageNamed:_str] forState:UIControlStateNormal];
116
117     }
118 }

效果图:

时间: 2024-10-10 11:19:03

UI拓展练习2----关灯游戏的相关文章

IOS 关灯游戏

一, 首先为window创建根视图控制器 在AppDelegate.h文件中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions //为window创建根视图控制器 RootViewController *rootViewController = [[RootViewController alloc]init]; self.wind

睡觉请关灯 &nbsp; 游戏模拟实现

 首先,关于这个的基本讨论已过一阶段了.现在就是合成版  以后做mcf java等的界面. 留了 一个bug就是 电脑解决完后 会多打印几个矩阵. 改进的地方,1:增加了一种电脑解法,         2:记录用了自增栈   #include"c.h" #include"c++.h" #define  MAX  5 #define N   10 typedef int  elem_type;   class Stack { public:   Stack()  { 

[某鸥实训记][objective-c][第二天][作业]黑白棋+关灯游戏

自己写的..所以可能没什么逻辑性...可能特别水... 环境为ios SDK8.0 选择的Simulator是iPhone6 创建ios SingleViewApplication..然后再ViewController.m中的代码就是全部了 1 // 2 // ViewController.m 3 // 黑白棋0908 4 // 5 // Created by ******* on 15/9/8. 6 // Copyright (c) 2015年 *******. All rights rese

【算法设计与分析基础】关灯游戏

① R.java  用于存储一些全局变量 package lightoff; public class R { /* public static int lightCondition[][] = { {1,0,0,1,1}, {1,0,0,1,1}, {1,0,0,1,1}, {1,0,0,1,1}, }; */ public static int lightCondition[][] = { {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; //灯面

小代码 链表实现关灯游戏2

/* cout << " *****************game by  ring light**************************" << endl;     cout << " **** 一个全开的循环串联灯链   为节约资源等待你的关灯动作******" << endl;     cout << " *****提示: 当选择一盏灯时,该灯及旁边的灯状态均会发生变化****&

关灯游戏

ViewController.h @property(strong,nonatomic)UIButton *button; @property(assign,nonatomic)int number; ViewController.m //每一排的个数 int index=5; //按钮的宽度 CGFloat buttonWidth=60; //按钮的高度 CGFloat buttonHeigth=60; //按钮的起始位置,到边界Y的位置 CGFloat buttonTop=20; //按钮的

UI拓展练习1----打地鼠

//打地鼠核心代码: 1 #import "ViewController.h" 2 #define Krandom arc4random()%(115-100+1)+100 3 @interface ViewController () 4 { 5 NSInteger _n; //记录当前的Krandom值 6 CGFloat _grade;//记录点击中的次数 7 NSInteger _counter;//记录循环次数 8 } 9 10 11 @property(nonatomic,

关灯游戏的实现

UIImage *ima = [UIImage imageNamed:@"1.png"]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { ButtonSubview * button = [ButtonSubview buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(5 + 63 * j, 100 + 63 * i, 58, 5

OC实现简单的关灯游戏

//将灯的图片在视图上布局,5行5列 for (int i = 0; i < 5; i++) { for (int j = 0 ; j < 5; j++) { UIButton *lightButton = [UIButton buttonWithType:UIButtonTypeSystem]; lightButton.frame = CGRectMake(i * 64, 120 + (j *64), 64, 64); [lightButton setBackgroundImage:[UII