首页 技术 正文
技术 2022年11月18日
0 收藏 306 点赞 3,077 浏览 5849 个字

就是点一下灯 它本身和周围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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903