关灯游戏源码(iOS)

时间:2023-03-09 19:27:51
关灯游戏源码(iOS)

就是点一下灯 它本身和周围4盏灯会变色

ViewController.m文件

#import "ViewController.h"
#import "UIView+change.h"
@interface ViewController ()
@property(assign,nonatomic)int totalColums; @property(assign,nonatomic)NSInteger tag;
@property(strong,nonatomic)UIButton *btn;
@end
@implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
CGFloat height=;
CGFloat width=;
self.view.tag=;
self.totalColums=; CGFloat marginX=(self.view.frame.size.width-width*self.totalColums)/(self.totalColums+);
CGFloat marginY=;
for (int i=; i<; i++) {
int row=i/self.totalColums;
int col=i%self.totalColums;
CGFloat appX=marginX+col*(width+marginX);
CGFloat appY=marginY+row*(height+marginY);
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(appX, appY, width, height)];
btn.backgroundColor=[UIColor redColor];
[btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
btn.tag=i;
[self.view addSubview:btn];
} }
/**
* 点击事件
*
*
*/
-(void)onClick:(UIButton *)btn{
[btn change];
self.btn=btn;
self.tag=btn.tag;
[self lie];
[self sigiao];
[self hang];
[self qita];
}
/**
* 最左边一列和最右边一列
*/
-(void)lie{ if ((self.tag%self.totalColums==||self.tag%self.totalColums==)&&(self.tag/self.totalColums!=&&self.tag/self.totalColums!=)) {
[[self.view viewWithTag:self.tag+self.totalColums] change];
[[self.view viewWithTag:self.tag-self.totalColums] change];
if (self.tag%self.totalColums==) {
[[self.view viewWithTag:self.tag+] change];
}else if(self.tag%self.totalColums==){
[[self.view viewWithTag:self.tag-] change];
}
}
}
/**
* 最上面一行和最下面一行
*/ -(void)hang{ if ((self.tag/self.totalColums==||self.tag/self.totalColums==)&&(self.tag%self.totalColums!=&&self.tag%self.totalColums!=)) {
[[self.view viewWithTag:self.tag-] change];
[[self.view viewWithTag:self.tag+] change];
if (self.tag/self.totalColums==) {
[[self.view viewWithTag:self.tag+self.totalColums] change];
}else if(self.tag/self.totalColums==){
[[self.view viewWithTag:self.tag-self.totalColums] change];
}
} }
/**
* 四个角
*/
-(void)sigiao{ if (self.tag==) {
[[self.view viewWithTag:self.tag+] change];
[[self.view viewWithTag:self.tag+self.totalColums] change];
}else if (self.tag==){
[[self.view viewWithTag:self.tag-] change];
[[self.view viewWithTag:self.tag+self.totalColums] change];
}else if(self.tag==-self.totalColums){
[[self.view viewWithTag:self.tag+] change];
[[self.view viewWithTag:self.tag-self.totalColums] change];
}else if(self.tag==){
[[self.view viewWithTag:self.tag-] change];
[[self.view viewWithTag:self.tag-self.totalColums] change];
}
}
/**
* 其他的
*/
-(void)qita{ if (self.tag/self.totalColums!=&&self.tag/self.totalColums!=&&self.tag%self.totalColums!=&&self.tag%self.totalColums!=) {
[[self.view viewWithTag:self.tag-self.totalColums]change];
[[self.view viewWithTag:self.tag-]change];
[[self.view viewWithTag:self.tag+self.totalColums]change];
[[self.view viewWithTag:self.tag+] change];
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
#import "ViewController.h"
#import "UIView+change.h"
@interface ViewController ()
@property(assign,nonatomic)int totalColums; @property(assign,nonatomic)NSInteger tag;
@property(strong,nonatomic)UIButton *btn;
@end
@implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
CGFloat height=;
CGFloat width=;
self.view.tag=;
self.totalColums=; CGFloat marginX=(self.view.frame.size.width-width*self.totalColums)/(self.totalColums+);
CGFloat marginY=;
for (int i=; i<; i++) {
int row=i/self.totalColums;
int col=i%self.totalColums;
CGFloat appX=marginX+col*(width+marginX);
CGFloat appY=marginY+row*(height+marginY);
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(appX, appY, width, height)];
btn.backgroundColor=[UIColor redColor];
[btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
btn.tag=i;
[self.view addSubview:btn];
} }
/**
* 点击事件
*
*
*/
-(void)onClick:(UIButton *)btn{
[btn change];
self.btn=btn;
self.tag=btn.tag;
[self lie];
[self sigiao];
[self hang];
[self qita];
}
/**
* 最左边一列和最右边一列
*/
-(void)lie{ if ((self.tag%self.totalColums==||self.tag%self.totalColums==)&&(self.tag/self.totalColums!=&&self.tag/self.totalColums!=)) {
[[self.view viewWithTag:self.tag+self.totalColums] change];
[[self.view viewWithTag:self.tag-self.totalColums] change];
if (self.tag%self.totalColums==) {
[[self.view viewWithTag:self.tag+] change];
}else if(self.tag%self.totalColums==){
[[self.view viewWithTag:self.tag-] change];
}
}
}
/**
* 最上面一行和最下面一行
*/ -(void)hang{ if ((self.tag/self.totalColums==||self.tag/self.totalColums==)&&(self.tag%self.totalColums!=&&self.tag%self.totalColums!=)) {
[[self.view viewWithTag:self.tag-] change];
[[self.view viewWithTag:self.tag+] change];
if (self.tag/self.totalColums==) {
[[self.view viewWithTag:self.tag+self.totalColums] change];
}else if(self.tag/self.totalColums==){
[[self.view viewWithTag:self.tag-self.totalColums] change];
}
} }
/**
* 四个角
*/
-(void)sigiao{ if (self.tag==) {
[[self.view viewWithTag:self.tag+] change];
[[self.view viewWithTag:self.tag+self.totalColums] change];
}else if (self.tag==){
[[self.view viewWithTag:self.tag-] change];
[[self.view viewWithTag:self.tag+self.totalColums] change];
}else if(self.tag==-self.totalColums){
[[self.view viewWithTag:self.tag+] change];
[[self.view viewWithTag:self.tag-self.totalColums] change];
}else if(self.tag==){
[[self.view viewWithTag:self.tag-] change];
[[self.view viewWithTag:self.tag-self.totalColums] change];
}
}
/**
* 其他的
*/
-(void)qita{ if (self.tag/self.totalColums!=&&self.tag/self.totalColums!=&&self.tag%self.totalColums!=&&self.tag%self.totalColums!=) {
[[self.view viewWithTag:self.tag-self.totalColums]change];
[[self.view viewWithTag:self.tag-]change];
[[self.view viewWithTag:self.tag+self.totalColums]change];
[[self.view viewWithTag:self.tag+] change];
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

类目文件UIView+change.h

#import <UIKit/UIKit.h>

@interface UIView (change)
/**
* 变颜色
*/
-(void)change;
@end

UIView+change.m

#import "UIView+change.h"

@implementation UIView (change)
-(void)change{
if (self.backgroundColor==[UIColor redColor]) {
self.backgroundColor=[UIColor blueColor];
}else{
self.backgroundColor=[UIColor redColor];
}
}
@end

运行效果

关灯游戏源码(iOS)