您的位置:首页 > 产品设计 > UI/UE

关灯游戏,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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: