2012-04-09 133 views
0

我有5X5個按鈕網格。如果我按下位置​​1上的按鈕然後按下位置2上的按鈕,我希望位置1的按鈕圖像移動到位置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; 

誰能幫助?在此先感謝.... :)

回答

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

有許多方法來獲得你的目標......

您可以使用此方法來更改按鈕圖像:

[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,用於管理要移動的圖像。如果你想給動畫

-(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; 
    } 
} 
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]; 

請像:

無論如何,你可以只使用一個方法,你所有的25個按鈕它

+0

這是好事,但使用這些方法中的iOS 4和更高氣餒。改爲使用基於塊的動畫方法。 – Beppe 2012-04-09 11:29:03

0

一種替代實現這一點,在創建按鈕時添加所有按鈕中的陣列和設置的每個按鈕tag.Then存儲的兩個按壓button.Use標籤先前壓制標籤使用[buttonarray objectAtIndex:previous.tag]讀取按鈕,並設定爲零,並以前按鈕的當前標籤設置背景圖像。