关灯游戏源码(iOS)
2024-09-16 00:09:06
就是点一下灯 它本身和周围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
运行效果
最新文章
- python sokct 包详解
- svg格式嵌入html中方法之一
- 刚开始用git遇到的无法提交变更的问题
- ABAP工作区,内表,标题行的定义和区别
- AngularJs的UI组件ui-Bootstrap分享(二)——Collapse
- OGG-01232 Receive TCP params error: TCP/IP error 104 (Connection reset by peer), endpoint:
- [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler
- ASP.NET jQuery 随笔 显示RadioButtonList成员选中的内容和值
- Web.config 文件中的 system.webServer
- Struts2 语法--action
- Halcon一日一练:创建三通道图像
- elasticsearch best_fields most_fields cross_fields从内在实现看区别——本质就是前两者是以field为中心,后者是词条为中心
- [js] 处理字符串换行造成的json解析失败
- 对浏览器攻击:MS10-002
- 【题解】 bzoj1088: [SCOI2005]扫雷Mine (神奇的做法)
- iOS SDK 从配置文件里读SDK。转化成class 可同时加载多个SDK
- 用tomcat发布自己的Java项目
- spoj TBATTLE 质因数分解+二分
- Flask从入门到精通之Flask-Moment本地化日期和时间
- php中查询时间
热门文章
- android 视频的缩略图 缓存机制和 异步加载缩略图
- Teehan &; Lax 发布 iOS 7 GUI PSD 模板,免费下载
- Android 学习笔记之AndBase框架学习(六) PullToRefrech 下拉刷新的实现
- TreeSet与TreeMap浅解
- [Tool] 源代码管理之Git
- Vector Clock/Version Clock
- 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制
- LeetCode129:Sum Root to Leaf Numbers
- php学习笔记:读取文档的内容,利用php修改文档内容
- Linux FTP配置文件说明