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