移动按钮图像从第一个按钮按下到第二个按钮

时间:2012-04-09 09:05:31

标签: iphone cocoa-touch ios4 logic

我有5X5个按钮网格。如果我按下位置​​1处的按钮然后按位置2处的按钮,我希望将position1的按钮图像移动到位置2处的按钮。如下面的示例所示,首先按下A1按钮,然后按下B2按钮。
实施例:

A1 button pressed

图:按下A1按钮

B2 button pressed

图:按下B2按钮

因此,整个过程包含两个步骤:

1. Prepare to move image when first button pressed
2. Move the image from first button to second button when the second button is pressed

我为5X5网格中显示的25个按钮设置了按钮标签。现在我无法找到采取所需行动的逻辑。

这些方法的逻辑是什么:

-(void)a1ButtonPressed:(id)sender
{

}

-(void)b2ButtonPressed:(id)sender
{

}

按钮上有这些标签:

A1.tag = 1;
B2.tag = 7;

有人可以帮忙吗?在此先感谢.... :)

4 个答案:

答案 0 :(得分:1)

只需对所有按钮使用一种方法,就像这样的简单逻辑

-(void)buttonPressed:(UIButton *)btn {
     if (buttonPressed) {//second press
         [initialBtn setImage:blankImage forControlState:UIControlStateNormal];
         [btn setImage:buttonImage forControlState:UIControlStateNormal];
         buttonPressed=NO;
     } else {//first press
         buttonImage=[btn imageForState:UIControlStateNormal];
         buttonPressed=YES;
         initialBtn=btn;
     }

在你的标题中你会有

 BOOL buttonPressed;
 UIImage *buttonImage;
 UIButton *initialBtn;

如果用户按两次相同的按钮取消他们的选择,这也会有效。

希望这能帮到你!

答案 1 :(得分:1)

有很多方法可以实现你的目标......

您可以使用此方法更改按钮图像:

[aButton setImage:[UIImage imageNamed:@"anImage"] forState:UIControlStateNormal];

您可以使用变量管理按钮图像。

然后:

-(void)a1ButtonPressed:(id)sender
{
    if (firstButton) {
        // Get image to move from A1
        imageToMove = [A1 imageForState:UIControlStateNormal];
        // Remove image from A1
        [A1 setImage:nil forState:UIControlStateNormal];
        firstButton = NO;
    } else {
        // Put new image to A1
        [A1 setImage:imageToMove forState:UIControlStateNormal];
        firstButton = YES;
    }

}

-(void)b2ButtonPressed:(id)sender
{
    if (firstButton) {
        // Get image to move from B2
        imageToMove = [A1 imageForState:UIControlStateNormal];
        // Remove image from B2
        [B2 setImage:nil forState:UIControlStateNormal];
        firstButton = NO;
    } else {
        // Put new image to B2
        [B2 setImage:imageToMove forState:UIControlStateNormal];
        firstButton = YES;
    }
}

等等......

fistButton是一个BOOL,用于控制按下的按钮是第一个还是第二个。 imageToMove是在其他地方声明的UIImage,用于管理要移动的图像。

无论如何,你可以只为你的所有25个按钮使用一种方法:

-(void)buttonPressed:(UIButton *)button
{
    if (firstButton) {
        // Get image to move from button
        imageToMove = [button imageForState:UIControlStateNormal];
        // Remove image from button
        [button setImage:nil forState:UIControlStateNormal];
        firstButton = NO;
    } else {
        // Put new image to button
        [button setImage:imageToMove forState:UIControlStateNormal];
        firstButton = YES;
    }
}

答案 2 :(得分:1)

如果你想让动画需要做UIView动画,那就是核心动画

[UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];
        //set animation value means the next postion where u want to move object 
        [UIView commitAnimations];

请喜欢它

答案 3 :(得分:0)

完成此操作的替代方法,在创建按钮时添加数组中的所有按钮并将每个按钮设置为标记。然后存储两个按下按钮的标记。使用前一个按下的标记,使用[buttonarray objectAtIndex: previous.tag]并设置为nil和当前标签设置上一个按钮的背景图像。

相关问题