UI03关灯游戏


1,首先新建视图控制器

2,在视图控制器中铺上5*5的小格,每个格都是一个新View,全部添加到视频控制器上

3,当点击时调用系统触摸方法,可以把实现关灯的方法写在触摸方法里


代码如下:

先铺格

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(35 + j * 50, 40 + i * 50, 50, 50)];
            [self.view addSubview:view1];
            view1.layer.borderWidth = 0.5;
            view1.backgroundColor = [UIColor whiteColor];
            view1.tag += 10000 + j + i * 100;
            [view1 release];
        }
    }
}


触摸

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    int array[] = {0, 1, -1, 100, -100};
    int count = sizeof(array) / sizeof(array[0]);
    
    for (int i = 0; i < count; i++) {
        UIView *view = (UIView *)[self.view viewWithTag:(touch.view.tag + array[i])];
        if ([view.backgroundColor isEqual:[UIColor whiteColor]]) {
            view.backgroundColor = [UIColor clearColor];
        } else if ([view.backgroundColor isEqual:[UIColor clearColor]]) {
            view.backgroundColor = [UIColor whiteColor];
        }
    }
    
}


关灯游戏之图片换图片:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(35 + j * 50, 40 + i * 50, 50, 50)];
            imageView.userInteractionEnabled = YES;
            [self.view addSubview:imageView];
            
            UIImage *image = [UIImage imageNamed:@"1.jpg"];
            imageView.image = image;
            imageView.layer.borderWidth = 0.5;
            imageView.tag += 10000 + j + i * 100;
            [imageView release];
        }
    }
}

// 触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    int array[] = {0, 1, -1, 100, -100};
    int count = sizeof(array) / sizeof(array[0]);
    UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"2.jpg"];
    
    for (int i = 0; i < count; i++) {
        UIImageView *view = (UIImageView *)[self.view viewWithTag:(touch.view.tag + array[i])];
        if ([view isKindOfClass:[UIImageView class]]) {
            
            if ([view.image isEqual:image1]) {
                view.image = image2;
            } else {
                view.image = image1;
            }
        }
    }
    
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值