这是一个翻牌子的游戏~~
在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的方法时,要进行逻辑判断~~