欢迎光临
我们一直在努力

关灯游戏源码(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=50; CGFloat width=50; self.view.tag=1000; self.totalColums=5; CGFloat marginX=(self.view.frame.size.width-width*self.totalColums)/(self.totalColums+1); CGFloat marginY=30; for (int i=0; i<45; 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==0||self.tag%self.totalColums==4)&&(self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8)) { [[self.view viewWithTag:self.tag+self.totalColums] change]; [[self.view viewWithTag:self.tag-self.totalColums] change]; if (self.tag%self.totalColums==0) { [[self.view viewWithTag:self.tag+1] change]; }else if(self.tag%self.totalColums==4){ [[self.view viewWithTag:self.tag-1] change]; } } } /** * 最上面一行和最下面一行 */ -(void)hang{ if ((self.tag/self.totalColums==0||self.tag/self.totalColums==8)&&(self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4)) { [[self.view viewWithTag:self.tag-1] change]; [[self.view viewWithTag:self.tag+1] change]; if (self.tag/self.totalColums==0) { [[self.view viewWithTag:self.tag+self.totalColums] change]; }else if(self.tag/self.totalColums==8){ [[self.view viewWithTag:self.tag-self.totalColums] change]; } } } /** * 四个角 */ -(void)sigiao{ if (self.tag==0) { [[self.view viewWithTag:self.tag+1] change]; [[self.view viewWithTag:self.tag+self.totalColums] change]; }else if (self.tag==4){ [[self.view viewWithTag:self.tag-1] change]; [[self.view viewWithTag:self.tag+self.totalColums] change]; }else if(self.tag==45-self.totalColums){ [[self.view viewWithTag:self.tag+1] change]; [[self.view viewWithTag:self.tag-self.totalColums] change]; }else if(self.tag==44){ [[self.view viewWithTag:self.tag-1] change]; [[self.view viewWithTag:self.tag-self.totalColums] change]; } } /** * 其他的 */ -(void)qita{ if (self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8&&self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4) { [[self.view viewWithTag:self.tag-self.totalColums]change]; [[self.view viewWithTag:self.tag-1]change]; [[self.view viewWithTag:self.tag+self.totalColums]change]; [[self.view viewWithTag:self.tag+1] 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=50; CGFloat width=50; self.view.tag=1000; self.totalColums=5; CGFloat marginX=(self.view.frame.size.width-width*self.totalColums)/(self.totalColums+1); CGFloat marginY=30; for (int i=0; i<45; 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==0||self.tag%self.totalColums==4)&&(self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8)) { [[self.view viewWithTag:self.tag+self.totalColums] change]; [[self.view viewWithTag:self.tag-self.totalColums] change]; if (self.tag%self.totalColums==0) { [[self.view viewWithTag:self.tag+1] change]; }else if(self.tag%self.totalColums==4){ [[self.view viewWithTag:self.tag-1] change]; } } } /** * 最上面一行和最下面一行 */ -(void)hang{ if ((self.tag/self.totalColums==0||self.tag/self.totalColums==8)&&(self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4)) { [[self.view viewWithTag:self.tag-1] change]; [[self.view viewWithTag:self.tag+1] change]; if (self.tag/self.totalColums==0) { [[self.view viewWithTag:self.tag+self.totalColums] change]; }else if(self.tag/self.totalColums==8){ [[self.view viewWithTag:self.tag-self.totalColums] change]; } } } /** * 四个角 */ -(void)sigiao{ if (self.tag==0) { [[self.view viewWithTag:self.tag+1] change]; [[self.view viewWithTag:self.tag+self.totalColums] change]; }else if (self.tag==4){ [[self.view viewWithTag:self.tag-1] change]; [[self.view viewWithTag:self.tag+self.totalColums] change]; }else if(self.tag==45-self.totalColums){ [[self.view viewWithTag:self.tag+1] change]; [[self.view viewWithTag:self.tag-self.totalColums] change]; }else if(self.tag==44){ [[self.view viewWithTag:self.tag-1] change]; [[self.view viewWithTag:self.tag-self.totalColums] change]; } } /** * 其他的 */ -(void)qita{ if (self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8&&self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4) { [[self.view viewWithTag:self.tag-self.totalColums]change]; [[self.view viewWithTag:self.tag-1]change]; [[self.view viewWithTag:self.tag+self.totalColums]change]; [[self.view viewWithTag:self.tag+1] 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)

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《关灯游戏源码(iOS)》
文章链接:https://www.456zj.com/41031.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址