Fork me on GitHub

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 }

 

效果图:

 

 

posted @ 2015-09-27 15:31  DengHuiCheng  阅读(248)  评论(0编辑  收藏  举报