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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值