码迷,mamicode.com
首页 > 其他好文 > 详细

关灯游戏

时间:2016-03-08 23:50:30      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!