Turn off the lights game

Source: Internet
Author: User

ViewController.h

@property (strong,nonatomic) UIButton *button;

@property (assign,nonatomic) int number;

Viewcontroller.m

The number of each row

int index=5;

Width of the button

CGFloat buttonwidth=60;

The height of the button

CGFloat buttonheigth=60;

The start position of the button to the position of the border y

CGFloat buttontop=20;

The start position of the button to the position of the boundary x

CGFloat buttontox= (Self.view.frame.size.width-buttonwidth*index)/(index+1);

CGFloat Buttontoy=buttontox;

Sets the number of calculated buttons, initialized with a value of 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);

}

}

Implementing Location methods

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

}

}

Ways to change colors

-(void) Change

{

if (Self.backgroundcolor==[uicolor Redcolor])

{

Self.backgroundcolor=[uicolor Bluecolor];

}

Else

{

Self.backgroundcolor=[uicolor Redcolor];

}

}

Turn off the lights game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.