有很多方法可以实现你的目标...
您可以使用此方法更改按钮图像:
[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;
}
}