强制按钮交换图像

时间:2011-03-03 17:42:54

标签: objective-c ipad ios


我正在研究iPad的匹配程序,当用户选择按钮时,图像被“揭开”,然后当用户选择第二个按钮时,另一个图像被揭开。然后我以编程方式检查匹配,如果没有,则将两个按钮图像恢复到其初始状态。

这个工作正常,除非没有进行匹配,切换发生得太快,以至于你没有时间看你有什么“未被发现”。我试图让它睡觉,但图像不会交换到未覆盖的状态......思考?

此代码如下:

//Take action on the tap of one of the buttons
if(isFirstSelection)
{
    firstSelection = [(UIButton *)sender tag];
    tempImageItem = [tileArray objectAtIndex:firstSelection];
    tempImage = [tempImageItem tileImage];
    firstSelectionName = [[NSString alloc] initWithString:[tempImageItem tileName]];
    [(UIButton *)sender setImage:tempImage forState:UIControlStateNormal];
    tempButton = sender;
    isFirstSelection = NO;       
}else{
    secondSelection = [(UIButton *)sender tag];
    tempImageItem = [tileArray objectAtIndex:secondSelection];
    tempImage = [tempImageItem tileImage];
    secondSelectionName = [[NSString alloc] initWithString:[tempImageItem tileName]];
    [(UIButton *)sender setImage:tempImage forState:UIControlStateNormal];

    //Two game pieces have been removed so check to see if they are a match
    if([firstSelectionName isEqualToString:secondSelectionName])
    {
        //Match found
        //do something

    }else{
        **//NO MATCH FOUND 
        [NSThread sleepForTimeInterval:3]; 
        //Display the checker board pieces again 
        [(UIButton *)sender setImage:[UIImage imageNamed:@"originalImage"] forState:UIControlStateNormal];** 

    }
    //Reset isFirstSelection Flag to YES for next selection
    isFirstSelection = YES;           
}

2 个答案:

答案 0 :(得分:0)

根据我的理解,您希望在3秒后放置按钮的原始图像,以便用户有时间查看发生的情况。 您应该查看NSTimer类以触发该代码

[(UIButton *)sender setImage:[UIImage imageNamed:@"originalImage"] forState:UIControlStateNormal];

从现在起3秒内。

答案 1 :(得分:0)

对于“在X秒内运行此代码”,我更喜欢:

[self performSelector:@selector(spinWheel:) withObject:[NSNumber numberWithUnsignedInt:0] afterDelay:delay];

无需使用计时器对象。