关灯游戏,UI教程第二节百行代码解决
2015-05-29 09:54
363 查看
#import
"AppDelegate.h"
#import
"MyButton.h"
@interface
AppDelegate
()
@end
@implementation
AppDelegate
-(void)dealloc
{
[_window
release];
[super
dealloc];
}
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
self.window
= [[[UIWindow
alloc]
initWithFrame:[[UIScreen
mainScreen]
bounds]]
autorelease];
self.window.backgroundColor
= [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
for (int
i = 0; i < 10; i++) {
for (int
j = 0; j < 10; j++) {
//
调用便利构造器,内存无需手动释放.
MyButton *button = [MyButton
buttonWithType:UIButtonTypeSystem];
button.frame
=
CGRectMake((32 * i), (100 + 32 * j), 32, 32);
button.tag
= 100 + i * 11 + j;
[button
setBackgroundImage:[UIImage
imageNamed:@"11.png"]
forState:UIControlStateNormal];
[button
addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
//
灯是否亮
button.light
=
NO;
[self.window
addSubview:button];
}
}
return
YES;
}
- (void)buttonAction:(MyButton
*)sender
{
// NSLog(@"%ld",(long)sender.tag);
//
父类调用子类对象,强制转换.
MyButton *button1 = (MyButton
*)[self.window
viewWithTag:(sender.tag
- 1)];
[self
arroundAction:button1];
MyButton *button2 = (MyButton
*)[self.window
viewWithTag:(sender.tag
+ 1)];
[self
arroundAction:button2];
MyButton *button3 = (MyButton
*)[self.window
viewWithTag:(sender.tag
- 11)];
[self
arroundAction:button3];
MyButton *button4 = (MyButton
*)[self.window
viewWithTag:(sender.tag
+ 11)];
[self
arroundAction:button4];
[self
arroundAction:sender];
}
- (void)arroundAction:(MyButton
*)sender
{
if (sender.light) {
[sender
setBackgroundImage:[UIImage
imageNamed:@"11.png"]
forState:UIControlStateNormal];
sender.light
=
NO;
} else {
[sender
setBackgroundImage:[UIImage
imageNamed:@"22.png"]
forState:UIControlStateNormal];
sender.light
=
YES;
}
}
"AppDelegate.h"
#import
"MyButton.h"
@interface
AppDelegate
()
@end
@implementation
AppDelegate
-(void)dealloc
{
[_window
release];
[super
dealloc];
}
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
self.window
= [[[UIWindow
alloc]
initWithFrame:[[UIScreen
mainScreen]
bounds]]
autorelease];
self.window.backgroundColor
= [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
for (int
i = 0; i < 10; i++) {
for (int
j = 0; j < 10; j++) {
//
调用便利构造器,内存无需手动释放.
MyButton *button = [MyButton
buttonWithType:UIButtonTypeSystem];
button.frame
=
CGRectMake((32 * i), (100 + 32 * j), 32, 32);
button.tag
= 100 + i * 11 + j;
[button
setBackgroundImage:[UIImage
imageNamed:@"11.png"]
forState:UIControlStateNormal];
[button
addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
//
灯是否亮
button.light
=
NO;
[self.window
addSubview:button];
}
}
return
YES;
}
- (void)buttonAction:(MyButton
*)sender
{
// NSLog(@"%ld",(long)sender.tag);
//
父类调用子类对象,强制转换.
MyButton *button1 = (MyButton
*)[self.window
viewWithTag:(sender.tag
- 1)];
[self
arroundAction:button1];
MyButton *button2 = (MyButton
*)[self.window
viewWithTag:(sender.tag
+ 1)];
[self
arroundAction:button2];
MyButton *button3 = (MyButton
*)[self.window
viewWithTag:(sender.tag
- 11)];
[self
arroundAction:button3];
MyButton *button4 = (MyButton
*)[self.window
viewWithTag:(sender.tag
+ 11)];
[self
arroundAction:button4];
[self
arroundAction:sender];
}
- (void)arroundAction:(MyButton
*)sender
{
if (sender.light) {
[sender
setBackgroundImage:[UIImage
imageNamed:@"11.png"]
forState:UIControlStateNormal];
sender.light
=
NO;
} else {
[sender
setBackgroundImage:[UIImage
imageNamed:@"22.png"]
forState:UIControlStateNormal];
sender.light
=
YES;
}
}
相关文章推荐
- 条款20:以const-reference传递替换by-value传递
- EasyUI-panel 内嵌页面上的js无法被执行
- 修改class默认编译目录build
- 检测到有潜在危险的 Request.Form 值
- Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- Dui界面布局无法响应鼠标点击消息响应
- Hadoop报错 " Message missing required fields: callId, status"解决方案
- UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案
- EasyUI Dialog with iFrame
- 学习笔记:UITabBarController使用详解
- JPA注解-@SequenceGenerator
- Mysql缓存Query Cache原理
- 关于EasyUIDataGrid查询功能
- 用c#开发微信 (8) 微渠道 - 推广渠道管理系统 3 UI设计及后台处理
- media query(媒体查询)和media type(媒体类型)
- Troubleshooting Guide ORA-3136: WARNING Inbound Connection Timed Out (文档 ID 465043.1)
- 前端框架 EasyUI (2)页面布局 Layout
- why constrained regression and Regularized regression equivalent
- 前端框架 EasyUI (1)熟悉一下EasyUI
- Problem A Number Sequence(KMP基础)