IOS 关灯游戏

一,

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

   在AppDelegate.h文件中

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

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

   在RootViewController.m文件中

  

- (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.

}

 

-(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];

            }

        }

    }

}

 

最后的结果是这样的:

  

   

转载于:https://www.cnblogs.com/taopengcun/p/3709678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值