Iphone 将按钮图像从按下的第一个按钮移动到按下的第二个按钮
我有5X5网格的按钮。如果我按下位置1处的按钮,然后按下位置2处的按钮,我希望位置1的按钮图像移动到位置2处的按钮。如下面的示例所示,首先按下A1按钮,然后按下B2按钮Iphone 将按钮图像从按下的第一个按钮移动到按下的第二个按钮,iphone,cocoa-touch,ios4,logic,Iphone,Cocoa Touch,Ios4,Logic,我有5X5网格的按钮。如果我按下位置1处的按钮,然后按下位置2处的按钮,我希望位置1的按钮图像移动到位置2处的按钮。如下面的示例所示,首先按下A1按钮,然后按下B2按钮示例: 图:按下A1按钮 图:按下B2按钮 因此,整个过程有两个步骤: 1. Prepare to move image when first button pressed 2. Move the image from first button to second button when the second button
示例:
图:按下A1按钮
图:按下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;
有人能帮忙吗?提前感谢……) 只需对所有按钮使用一种方法,简单的逻辑如下
-(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;
如果用户按下同一按钮两次,取消选择,这也会起作用
希望这对你有帮助 只需对所有按钮使用一种方法,简单的逻辑如下
-(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;
如果用户按下同一按钮两次,取消选择,这也会起作用
希望这对你有帮助 有很多方法可以实现你的目标 您可以使用此方法更改按钮图像:
[aButton setImage:[UIImage imageNamed:@"anImage"] forState:UIControlStateNormal];
您可以使用变量管理按钮图像
然后:
等等
fistButton
是一个布尔值,用于控制按下的按钮是第一个还是第二个。
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;
}
}
有很多方法可以实现你的目标 您可以使用此方法更改按钮图像:
[aButton setImage:[UIImage imageNamed:@"anImage"] forState:UIControlStateNormal];
您可以使用变量管理按钮图像
然后:
等等
fistButton
是一个布尔值,用于控制按下的按钮是第一个还是第二个。
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;
}
}
如果你想给动画,需要做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];
请喜欢它如果你想给动画,需要做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];
请喜欢它另一种方法是,在创建按钮时将所有按钮添加到一个数组中,并将每个按钮设置为一个标记。然后存储两个按钮的标记。使用上一个按下的标记使用[buttonarray对象索引:previous.tag]读取按钮并设置为零,并设置为当前标记设置上一个按钮的背景图像 另一种方法是,在创建按钮时将所有按钮添加到一个数组中,并将每个按钮设置为标签。然后存储两个按钮的标签。使用上一个按下的标签使用[buttonarray objectAtIndex:previous.tag]读取按钮,并将其设置为零,并将其设置为上一个按钮的当前标签集背景图像 这很好,但在iOS 4及更高版本中不鼓励使用这些方法。使用基于块的动画方法。这很好,但在iOS 4及更高版本中不鼓励使用这些方法。请改用基于块的动画方法。