UI关灯游戏 代码
2014-04-09 21:00
393 查看
#import "GameViewController.h"
@interface
GameViewController ()
@end
@implementation GameViewController
- (void) dealloc
{
[_nameLabel
release];
_nameLabel =
nil;
[super
dealloc];
}
- (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.
_nameLabel =[[UILabel
alloc] initWithFrame:CGRectMake(60,
20,
200, 40)];
[_nameLabel
setBackgroundColor:[UIColor
whiteColor]];
[_nameLabel
setText:@"白雪公主的黑暗骑士丶"];
UIFont * font = [UIFont
boldSystemFontOfSize:20];
[_nameLabel
setFont:font];
[_nameLabel
setTextAlignment:NSTextAlignmentCenter];
[_nameLabel
setTextColor:[UIColor
redColor]];
[self.view
addSubview:_nameLabel];
[_nameLabel
release];
UILabel * ziLabel = [[UILabel
alloc] initWithFrame:CGRectMake(60,
420, 200,
40)];
[ziLabel setBackgroundColor:[UIColor
whiteColor]];
[ziLabel
setText:@"小火龙丶就决定是你了"];
UIFont * font1 = [UIFont
boldSystemFontOfSize:20];
[ziLabel
setFont:font1];
[ziLabel setTextAlignment:NSTextAlignmentCenter];
[ziLabel setTextColor:[UIColor
redColor]];
[self.view
addSubview:ziLabel];
[ziLabel
release];
[self
creatImageButton];
}
- (void) creatImageButton
{
int tag = 1;
for (int i =
0; i < 7; i++) {
for (int j =
0; j < 6; j++) {
UIButton * button = [[UIButton
alloc] initWithFrame:CGRectMake(10 +
50 * j, 70 +
50 * i, 50,
50)];
[button setImage:[UIImage
imageNamed:@"321.png"]
forState:UIControlStateNormal];
[button setImage:[UIImage
imageNamed:@"000.png"]
forState:UIControlStateSelected];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button
setTag:tag++];
[self.view
addSubview:button];
[button
release];
}
}
}
- (void) buttonAction:(UIButton*)sender
{
NSLog(@"%d",sender.tag);
[sender
setSelected:![sender
isSelected]];
if ((sender.tag %
6 != 1) ) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag-1];
[abutton
setSelected:![abutton
isSelected]];
}
if ((sender.tag %
6 != 0) ) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag+1];
[abutton
setSelected:![abutton
isSelected]];
}
if (sender.tag >
6) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag-6];
[abutton
setSelected:![abutton
isSelected]];
}
if (sender.tag <
37) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag+6];
[abutton
setSelected:![abutton
isSelected]];
}
}
---------------------------------分割线---------------------------------------
GameViewController.h
#import <UIKit/UIKit.h>
@interface GameViewController :
UIViewController
//显示游戏名
@property (retain,
nonatomic) UILabel * nameLabel;
@end
---------------------------------分割线---------------------------------------
#import "AppDelegate.h"
#import "GameViewController.h"
@implementation AppDelegate
- (void) dealloc
{
[_window
release];
_window = nil;
[super
dealloc];
}
- (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
whiteColor];
GameViewController * game = [[GameViewController
alloc]
init];
[self.window
setRootViewController:game];
[self.window
makeKeyAndVisible];
return
YES;
}
@interface
GameViewController ()
@end
@implementation GameViewController
- (void) dealloc
{
[_nameLabel
release];
_nameLabel =
nil;
[super
dealloc];
}
- (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.
_nameLabel =[[UILabel
alloc] initWithFrame:CGRectMake(60,
20,
200, 40)];
[_nameLabel
setBackgroundColor:[UIColor
whiteColor]];
[_nameLabel
setText:@"白雪公主的黑暗骑士丶"];
UIFont * font = [UIFont
boldSystemFontOfSize:20];
[_nameLabel
setFont:font];
[_nameLabel
setTextAlignment:NSTextAlignmentCenter];
[_nameLabel
setTextColor:[UIColor
redColor]];
[self.view
addSubview:_nameLabel];
[_nameLabel
release];
UILabel * ziLabel = [[UILabel
alloc] initWithFrame:CGRectMake(60,
420, 200,
40)];
[ziLabel setBackgroundColor:[UIColor
whiteColor]];
[ziLabel
setText:@"小火龙丶就决定是你了"];
UIFont * font1 = [UIFont
boldSystemFontOfSize:20];
[ziLabel
setFont:font1];
[ziLabel setTextAlignment:NSTextAlignmentCenter];
[ziLabel setTextColor:[UIColor
redColor]];
[self.view
addSubview:ziLabel];
[ziLabel
release];
[self
creatImageButton];
}
- (void) creatImageButton
{
int tag = 1;
for (int i =
0; i < 7; i++) {
for (int j =
0; j < 6; j++) {
UIButton * button = [[UIButton
alloc] initWithFrame:CGRectMake(10 +
50 * j, 70 +
50 * i, 50,
50)];
[button setImage:[UIImage
imageNamed:@"321.png"]
forState:UIControlStateNormal];
[button setImage:[UIImage
imageNamed:@"000.png"]
forState:UIControlStateSelected];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button
setTag:tag++];
[self.view
addSubview:button];
[button
release];
}
}
}
- (void) buttonAction:(UIButton*)sender
{
NSLog(@"%d",sender.tag);
[sender
setSelected:![sender
isSelected]];
if ((sender.tag %
6 != 1) ) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag-1];
[abutton
setSelected:![abutton
isSelected]];
}
if ((sender.tag %
6 != 0) ) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag+1];
[abutton
setSelected:![abutton
isSelected]];
}
if (sender.tag >
6) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag-6];
[abutton
setSelected:![abutton
isSelected]];
}
if (sender.tag <
37) {
UIButton * abutton = (UIButton*)[self.view
viewWithTag:sender.tag+6];
[abutton
setSelected:![abutton
isSelected]];
}
}
---------------------------------分割线---------------------------------------
GameViewController.h
#import <UIKit/UIKit.h>
@interface GameViewController :
UIViewController
//显示游戏名
@property (retain,
nonatomic) UILabel * nameLabel;
@end
---------------------------------分割线---------------------------------------
#import "AppDelegate.h"
#import "GameViewController.h"
@implementation AppDelegate
- (void) dealloc
{
[_window
release];
_window = nil;
[super
dealloc];
}
- (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
whiteColor];
GameViewController * game = [[GameViewController
alloc]
init];
[self.window
setRootViewController:game];
[self.window
makeKeyAndVisible];
return
YES;
}
相关文章推荐
- 【转】dp,px,pt,sp的区别与转换
- UITextField知识点大全
- Android 线程监控以及线程刷新主UI的方法
- 继承System.Web.UI.Page的页面基类
- UISearchBar控件
- UI 视图控制器
- HDU_1297Children’s Queue
- continue和break的特殊用法。
- Driver porting: the workqueue interface.
- UIResponder学习
- Equinox加载Bundle Class的实现
- 【LeetCode练习题】Permutation Sequence
- L298N和arduino的正确连法
- DUIEngine使用Visual Studio 2010编译Debug_Dll版有关Error MSB3073错误解决方案
- “Guess the number” game
- vi/vim使用进阶: 剑不离手 – quickfix
- pcDuino上使用3G无线上网卡(华为E303s)
- 和我一起来学iOS(四)UIView及其子类(下)UITableView
- 和我一起来学iOS(三)UIView及其子类(上) 在开始前,我想大家应该先读一读 Mac OS X 背后的故事,是一篇非常不错的文章。了解曾经发生的过去,才能对现在为什么会是这样有更深刻的认识。
- Mysql监控-自带工具Query Profiler的使用