我正在使用Objective-C在iOS上制作匹配游戏
有一组12张卡片(3列4行)是UIButton
连接到IBOutletCollection
的数组,问题是当你点击第一张卡片时,你也可以同时点击其他卡片,但要点击的允许卡必须只有2张卡。如果分接的卡大于1且小于2,我该如何防止卡的敲击。
答案 0 :(得分:1)
//One action for all buttons
-(void)buttonAction:(UIButton*)button
{
//if user select three button at a time, dont do further operations
if (previousButton && currentButton) {
return;
}
//previous button is the first button
if(previousButton==nil)
{
previousButton=button;
}
//current button is the second button
else if(currentButton==nil)
{
currentButton=button;
}
//if button is selected, set button as not selected and vice versa
if([button isSelected]==NO)
{
[button setSelected:YES];
}
else
{
[button setSelected:NO];
}
//if user press the same button again and again
if (currentButton==previousButton)
{
currentButton=nil;
if([previousButton isSelected]==NO)
{
previousButton=nil;
}
return;
}
else if((currentButton!=previousButton)&&(currentButton!=nil))
{
//both button tags are same, that means both selected state images are same
if(previousButton.tag==currentButton.tag)
{
[self performSelector:@selector(delay) withObject:nil afterDelay:0.5];
}
else
{
[self performSelector:@selector(delayTwo) withObject:nil afterDelay:0.5];
}
}
}
-(void)delay
{
//[currentButton setHidden:YES];
//[previousButton setHidden:YES];
currentButton=nil;
previousButton=nil;
score++;
NSLog(@"Score %d",score);
//the final stage, that means left only two buttons
if(score==6)
{
//[self endGame];
}
}
-(void)delayTwo
{
[currentButton setSelected:NO];
[previousButton setSelected:NO];
currentButton=nil;
previousButton=nil;
}
在viewDidLoad中,指定所有选中的按钮&您需要的正常状态图像或标题
答案 1 :(得分:0)
我不是100%肯定你在这里得到了什么,但你可以尝试使用多个UITapGestureRecognizer实例。你可以有一个用于1个点击,一个用于2,一个用于3+。这样,您可以选择1或2个触摸卡,但如果有3个或更多,则忽略它。