html关灯游戏,关灯游戏

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;

//按钮的起始位置,到边界X的位置

CGFloat buttontoX=(self.view.frame.size.width-buttonWidth*index)/(index+1);

CGFloat buttontoY=buttontoX;

//设置计算按钮的个数,初始化赋值为1

self.number=1;

for (int i=0; i<45; i++)

{

int X=i%index;

int Y=i/index;

self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

CGFloat buttonX=(buttontoX+buttonWidth)*X+buttontoX;

CGFloat buttonY=(buttontoY+buttonHeigth)*Y+buttontoY+buttonTop;

self.button.frame=CGRectMake(buttonX, buttonY, buttonWidth, buttonHeigth);

self.button.backgroundColor=[UIColor redColor];

self.button.layer.cornerRadius=30;

[self.button addTarget:self action:@selector(testposition:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.button];

self.button.tag=self.number++;

NSLog(@"tag=%ld",(long)self.button.tag);

}

}

//实现位置方法

-(void)testposition:(UIButton *)Button

{

self.button=Button;

int num=(int)self.button.tag;

if (num==1)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num==5)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num==41)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+1] change];

}

else if(num==45)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num-5] change];

}

else if(num%5==1)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num%5==0)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num+5] change];

}

else if(num%5==1)

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num-5] change];

}

else

{

[[self.view viewWithTag:num] change];

[[self.view viewWithTag:num-1] change];

[[self.view viewWithTag:num+1] change];

[[self.view viewWithTag:num-5] change];

[[self.view viewWithTag:num+5] change];

}

}

//改变颜色的方法

-(void)change

{

if (self.backgroundColor==[UIColor redColor])

{

self.backgroundColor=[UIColor blueColor];

}

else

{

self.backgroundColor=[UIColor redColor];

}

}

原文:http://www.cnblogs.com/layios/p/5256301.html

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注