LZAppDelegate.m
#import "LZAppDelegate.h"
#import "MainViewController.h"
@implementation LZAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor yellowColor];
MainViewController *MainVC = [[MainViewController alloc]init];
self.window.rootViewController = MainVC;
[MainVC release];
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.m
#import "MainViewController.h"
#import "BView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 计算每个view的坐标
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
BView *View = [[BView alloc]initWithFrame:CGRectMake(10 + i * 40, 30 + j * 45, 30, 30)];
View.backgroundColor = [UIColor redColor];
if (i == 6 && j == 0) {
View.tag = 6;
} else {
View.tag = j * 6 + i;
}
[self.view addSubview:View];
}
}
}
- (void)dealloc
{
[self.view release];
[super dealloc];
}
BView.m
#import "BView.h"
@implementation BView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 求出上下左右每个view的tag值
_TopView = [self.window viewWithTag:self.tag - 6];
_RightView = [self.window viewWithTag:self.tag + 1];
_LeftView = [self.window viewWithTag:self.tag - 1];
_DownView = [self.window viewWithTag:self.tag + 6];
if (self.backgroundColor == [UIColor redColor]) {
self.backgroundColor = [UIColor blackColor];
} else if (self.backgroundColor == [UIColor blackColor]) {
self.backgroundColor = [UIColor redColor];
}
// 判断边界值 确定变灯的位置
if (self.tag % 6 != 1) {
if (_LeftView.backgroundColor == [UIColor redColor]) {
_LeftView.backgroundColor = [UIColor blackColor];
}else if (_LeftView.backgroundColor == [UIColor blackColor]) {
_LeftView.backgroundColor = [UIColor redColor];
}
} if (self.tag % 6 != self.tag) {
if (_TopView.backgroundColor == [UIColor redColor]) {
_TopView.backgroundColor = [UIColor blackColor];
} else if (_TopView.backgroundColor == [UIColor blackColor]) {
_TopView.backgroundColor = [UIColor redColor];
}
} if (self.tag % 6 != 0) {
if (_RightView.backgroundColor == [UIColor redColor]) {
_RightView.backgroundColor = [UIColor blackColor];
} else if (_RightView.backgroundColor == [UIColor blackColor]) {
_RightView.backgroundColor = [UIColor redColor];
}
} if (self.tag < 37) {
if (_DownView.backgroundColor == [UIColor redColor]) {
_DownView.backgroundColor = [UIColor blackColor];
} else if (_DownView.backgroundColor == [UIColor blackColor]) {
_DownView.backgroundColor = [UIColor redColor];
}
}
}