zoukankan      html  css  js  c++  java
  • 关灯游戏

    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];

        }

    }

  • 相关阅读:
    奇异值分解(SVD)详解
    深度学习中常用的优化方法
    一年了,走了一些弯路,是时候回归了,介绍下深度学习中常见的降低过拟合的方法
    softmax与logistic关系
    Java [Leetcode 387]First Unique Character in a String
    Java [Leetcode 384]Shuffle an Array
    Java [Leetcode 167]Two Sum II
    用stack实现min stack
    bootstrap, boosting, bagging 几种方法的联系
    一道常被人轻视的前端JS面试题
  • 原文地址:https://www.cnblogs.com/layios/p/5256301.html
Copyright © 2011-2022 走看看