需求:
有一个阵列按钮,按钮拥有两种颜色,当点击按钮时候,该按钮改变自己的颜色,同时围绕这个那就的上下左右按钮同时转换颜色,当素有颜色都变成与初始化相反的颜色时候,玩家胜利.
设计思路:
(1)实例化一个阵列的数组,装载到的一个数组里面,同时设置好UIButton的tag值,同时利用opaque透明属性来记录每个按钮独自的状态.
(2)需要处理情况:
1.左上角一个按钮:没有上/左
2.右上角一个按钮:没有上/右
3.左下角一个按钮:没有左/下
4.右下角一个按钮:没有右/下
5.除了左上角和右上角的第一行
6.除了左下角和右下角的最后一行
7.除了左上角和左下角的第一列
8.除了右上角和右下角 的最后一列
9.除了以上情况中间的按钮
(3)判断使用本按钮的tag
左:btn.tab%mod-1
右:btn.tab%mod+1
上:btn.tab-mod>0
下:btn.tag+mod<0
(4)根据opaque的值来切换按钮的颜色
创建一个single>
// // ViewController.h // TogleLight // DRAGON // Created by 5016 on 13-12-25. // Copyright (c) 2013年 dradon. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIButton *button; NSMutableArray *btnArr; } @end
// // ViewController.m // TogleLight // // Created by 5016 on 13-12-25. // Copyright (c) 2013年 dradon. All rights reserved. // #import "ViewController.h" #import "HandleMag.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; btnArr = [[NSMutableArray alloc]init]; [self initControl]; } -(void)initControl { [self initBtn]; } -(void)initBtn { int tagNum = 1001; for (int i = 0; i<ROW; i++) { for (int j = 0; j<MOD; j++) { button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundColor:[UIColor orangeColor]]; /*************************************/ [button setFrame:CGRectMake(8+63*j,13+63*i,50,50)]; [button setTag:tagNum++]; button.opaque = YES; [btnArr addObject:button]; //绑定事件 [button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:button]; [button release]; /*************************************/ } } } //按键响应事件 -(void)onclick:(UIButton*)btn { HandleMag *handlemsg = [[HandleMag alloc]init]; [handlemsg onClick:btnArr andBtn:btn]; [handlemsg release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
// // HandleMag.h // TogleLight // // Created by 5016 on 13-12-25. // Copyright (c) 2013年 dradon. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> //可以自定义行数与列数 #define MOD 5 #define ROW 7 #define MAX_NUM (ROW*MOD) #define MIN_NUM 0 @interface HandleMag : NSObject -(void)onClick:(NSMutableArray*)btnArr andBtn:(UIButton*)btn; @end
// // HandleMag.m // TogleLight // // Created by 5016 on 13-12-25. // Copyright (c) 2013年 dradon. All rights reserved. // #import "HandleMag.h" @implementation HandleMag /* *处理按键事件 */ -(void)onClick:(NSMutableArray*)btnArr andBtn:(UIButton*)btn { for (int i = 0; i< MAX_NUM; i++) { if (i == btn.tag-1001) { NSLog(@"btn.tag = %d",i); [self handleMSG:btnArr andTag:i]; } } } //设置颜色 -(void)setColor:(UIButton*)btn { if (btn.opaque) { // NSLog(@"YES"); [btn setBackgroundColor:[UIColor blackColor]]; btn.opaque = NO; }else{ // NSLog(@"NO"); [btn setBackgroundColor:[UIColor orangeColor]]; btn.opaque = YES; } } //处理逻辑 -(void)handleMSG:(NSMutableArray*)btnArr andTag:(int)tag { //上 if (tag+1-MOD>0 && tag+1+MOD<=MAX_NUM) { if ((tag+1)%MOD == 1) { NSLog(@"你点击了第一列"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; //UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; [self setColor:upBtn]; [self setColor:downBtn]; //[self setColor:leftBtn]; [self setColor:rightBtn]; }else if((tag+1)%MOD == 0){ NSLog(@"你点击了最后一列"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; //UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; [self setColor:upBtn]; [self setColor:downBtn]; [self setColor:leftBtn]; //[self setColor:rightBtn]; }else{ NSLog(@"你点击了中间行"); //设置背景颜色 //NSLog(@"TAG = %d",((UIButton*)[btnArr objectAtIndex:tag]).tag-1001); //NSLog(@"===%d",((UIButton*)[btnArr objectAtIndex:tag]).opaque); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; [self setColor:upBtn]; [self setColor:downBtn]; [self setColor:leftBtn]; [self setColor:rightBtn]; } }else if(tag+1+MOD > MAX_NUM){//最后一行 if ((tag+1)%MOD == 1) { NSLog(@"你点击了第一列"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; //UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; //UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; [self setColor:upBtn]; //[self setColor:downBtn]; //[self setColor:leftBtn]; [self setColor:rightBtn]; }else if((tag+1)%MOD == 0){ NSLog(@"你点击了最后一列"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; //UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; //UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; [self setColor:upBtn]; //[self setColor:downBtn]; [self setColor:leftBtn]; //[self setColor:rightBtn]; }else{ NSLog(@"你点击了最后一行"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; //UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; [self setColor:upBtn]; //[self setColor:downBtn]; [self setColor:leftBtn]; [self setColor:rightBtn]; } } else{//在第一行 if ((tag+1)%MOD == 1) { NSLog(@"你点击了第一列"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; //UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; //UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; //[self setColor:upBtn]; [self setColor:downBtn]; //[self setColor:leftBtn]; [self setColor:rightBtn]; }else if((tag+1)%MOD == 0){ NSLog(@"你点击了最后一列"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; //UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; //UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; //[self setColor:upBtn]; [self setColor:downBtn]; [self setColor:leftBtn]; //[self setColor:rightBtn]; }else{ NSLog(@"你点击了第一行"); UIButton* centerBtn = (UIButton*)[btnArr objectAtIndex:tag]; //UIButton* upBtn = (UIButton*)[btnArr objectAtIndex:tag-MOD]; UIButton* downBtn = (UIButton*)[btnArr objectAtIndex:tag+MOD]; UIButton* leftBtn = (UIButton*)[btnArr objectAtIndex:tag-1]; UIButton* rightBtn = (UIButton*)[btnArr objectAtIndex:tag+1]; [self setColor:centerBtn]; //[self setColor:upBtn]; [self setColor:downBtn]; [self setColor:leftBtn]; [self setColor:rightBtn]; } } } @end