iOS 开发值翻牌子游戏

4 篇文章 0 订阅

这是一个翻牌子的游戏~~

在window上添加了一个imageView,设置了一张图片:在这张图片上调添加了足够多的button;具体如代码所示:

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"green.jpg"]];

    imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.window.bounds),

                                CGRectGetHeight(self.window.bounds));

    [self.window addSubview:imageView];

    [imageView release];

    

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

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

            UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

            CGFloat width = (CGRectGetWidth(self.window.bounds) - 5 )/ 4;

            CGFloat height= (CGRectGetHeight(self.window.bounds) -8) /7;

            button1.frame = CGRectMake(1 + (width+1)*j, 1 + (height+1)*i, width, height);

            button1.tag = 10*i+j+2;

            button1.backgroundColor = [UIColor grayColor];

//            button1.backgroundColor = [UIColor colorWithWhite:1 alpha:1];

            [button1 addTarget:self action:@selector(handleUp:) forControlEvents:UIControlEventTouchDown];

            [self.window addSubview:button1];

        }

    }

    


实现button的点击方法:

- (void)handleUp:(UIButton *)sender

{

    sender.alpha = (sender.alpha ==1 ) ? 0.1 :1;

    UIButton *leftButton = (UIButton *)[self.window viewWithTag:sender.tag - 1];

    leftButton.alpha = (leftButton.alpha == 1) ? 0.1 : 1;

    UIButton *rightButton = (UIButton *)[self.window viewWithTag:sender.tag +1];

    rightButton.alpha = (rightButton.alpha == 1) ? 0.1 :1;

    UIButton *upButton = (UIButton *)[self.window viewWithTag:sender.tag - 10];

    upButton.alpha = (upButton.alpha == 1) ? 0.1 : 1;

    UIButton *downButton = (UIButton *)[self.window viewWithTag:sender.tag + 10];

    downButton.alpha = (downButton.alpha ==1) ? 0.1 : 1;

}

 根据tag值,改变与之对应的alpa值,呈现出效果~~ 

 这里稍微难的是整个逻辑的形成以及触发button的方法时,要进行逻辑判断~~


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值