IOS 关灯游戏

时间:2014-05-07 20:18:38   收藏:0   阅读:398

一,

   首先为window创建根视图控制器

   在AppDelegate.h文件中

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

bubuko.com,布布扣
//为window创建根视图控制器
    RootViewController *rootViewController = [[RootViewController alloc]init];
    self.window.rootViewController = rootViewController;
bubuko.com,布布扣

   在RootViewController.m文件中

  

bubuko.com,布布扣

- (void)viewDidLoad

{

    int count = 0;//定义count计数 tag标记存值

    UIImage *one = [UIImage imageNamed:@"h.png"];//设置一个图片

    for (int i=0; i<7; i++) {

        for (int j=0; j<5; j++) {

            count++;

            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame = CGRectMake(60*j, 30+i*60, 48, 48);

            button.tag = count;

            [button setImage:one forState:UIControlStateNormal];

            [button addTarget:selfaction:@selector(touch:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:button];

        }

        count+=5;//控制tag标记,实现下一行的tag标记

    }

    [superviewDidLoad];

// Do any additional setup after loading the view.

}

 

bubuko.com,布布扣
bubuko.com,布布扣

-(void)touch:(id)sender

{

    UIButton *button = (UIButton*)sender;

    for (int i=0; i<=65; i++) {

        UIButton *current = (UIButton*)[self.view viewWithTag:i];//设置当前按钮的点击事件

        //下面是控制tag左右,上下

        if ((i==button.tag-1)||(i==button.tag+1)||(i==button.tag-10)||(i==button.tag+10)) {

            if (current.currentImage ==[UIImage imageNamed:@"h.png"]) {

                [current setImage:[UIImageimageNamed:@"g.png"] forState:UIControlStateNormal];

            }

            else

            {

                [current setImage:[UIImageimageNamed:@"h.png"] forState:UIControlStateNormal];

            }

        }

    }

}

 

bubuko.com,布布扣

最后的结果是这样的:

  bubuko.com,布布扣

   

IOS 关灯游戏,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!