要求
實現(xiàn)開燈時將上下左右的燈都打開,關(guān)燈時將上下左右的燈都關(guān)閉,當(dāng)是在最前一排或在最后一排時析孽,注意前面沒有燈開
.h文件
#import "ViewController.h"
@interface ViewController ()
@property(strong,nonatomic)UIButton *button;
@property(copy,nonatomic)NSString *str;
@property(copy,nonatomic)NSString *str1;
@property(assign,nonatomic)CGRect rect;
@property(assign,nonatomic)BOOL a;
@end
.m文件
- (void)viewDidLoad {
[super viewDidLoad];
int indxe=5;
CGFloat appW=60;
CGFloat appH=60;
CGFloat appjxX=(self.view.frame.size.width-appW*indxe)/(indxe+1);
CGFloat appjxY=appjxX;
CGFloat apptopY=20;
九宮格循環(huán)建立按鈕
for (int i=0; i<45; i++) {
int X=i%indxe;
int Y=i/indxe;
self.button=[[UIButton alloc]initWithFrame:CGRectMake((appjxX+appW)*X+appjxX, (appH+appjxY)*Y+appjxY+apptopY, appW, appH)];
self.str=[NSString stringWithFormat:@"1.jpg"];
self.str1=[NSString stringWithFormat:@"2.jpg"];
設(shè)置按鈕低量嘲叔、高量圖片
[self.button setBackgroundImage:[UIImage imageNamed:self.str] forState:(UIControlStateNormal)];
[self.button setBackgroundImage:[UIImage imageNamed:self.str1] forState:UIControlStateHighlighted];
設(shè)置每個按鈕的唯一標(biāo)識
self.button.tag=i;
[self.button addTarget:self action:@selector(change:) forControlEvents:(UIControlEventTouchDown)];
[self.view addSubview:self.button];
}
}
點擊按鈕需要實現(xiàn)的方法
-(void)change:(UIButton *)button
{
int i=(int)button.tag;
定義數(shù)組执解,將上下左右的按鈕一一標(biāo)識
int arr[4]={i+1,i-1,i-5,i+5};
判斷是否是第一列
if (i%5==0) {
arr[1]=i+1;
}
判斷是否是最后一列
else if (i%5==4)
{
arr[0]=i-1;
}
如果處于低量狀態(tài)砸琅,那么自己和周圍的全變紅
if (self.a==YES) {
[self red:button];
for (int i=0; i<=4; i++) {
self.button=[[UIButton alloc]init];
self.button.frame=[self.view viewWithTag:arr[i]].frame;
[self red:self.button];
[self.view addSubview:self.button];
}
}
關(guān)燈效果甸私,在關(guān)燈時荣挨,將周圍的燈全部關(guān)掉
else
{
判斷是否是第一列
if (i%5==0) {
[self yea:button];
self.button=[self.view viewWithTag:i+1];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i+1];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i-5];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i+5];
[self yea:self.button];
[self.view addSubview:self.button];
}
判斷是否是最后一列
else if (i%5==4)
{
[self yea:button];
self.button=[self.view viewWithTag:i-1];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i-1];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i-5];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i+5];
[self yea:self.button];
[self.view addSubview:self.button];
}
else
{
[self yea:button];
self.button=[self.view viewWithTag:i+1];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i-1];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i-5];
[self yea:self.button];
[self.view addSubview:self.button];
self.button=[self.view viewWithTag:i+5];
[self yea:self.button];
[self.view addSubview:self.button];
}
}
}
變?yōu)榧t色方法
-(void)red:(UIButton *)button
{
[button setBackgroundImage:[UIImage imageNamed:self.str1] forState:UIControlStateNormal];
self.a=NO;
}
變?yōu)辄S色方法
-(void)yea:(UIButton *)button
{
[button setBackgroundImage:[UIImage imageNamed:self.str] forState:UIControlStateNormal];
self.a=YES;
}
圖片顯示效果
屏幕快照 2016-03-08 09.19.27 PM.png
屏幕快照 2016-03-08 09.19.42 PM.png
屏幕快照 2016-03-08 09.20.07 PM.png
屏幕快照 2016-03-08 09.20.44 PM.png
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者