2012-04-09 8 views
0

私は5X5グリッドのボタンを持っています。ポジション1のボタンを押してからポジション2のボタンを押すとポジション1のボタン画像をポジション2のボタンに移動します。下の例のように、A1ボタンを押してからB2ボタンを押します。
例:

A1 button pressed最初のボタンが押されてから2番目のボタンが押されたボタン画像を移動する

図:A1ボタン

B2 button pressed

押さ図:B2ボタン押下

したがって、プロセス全体は、2つのステップがあります

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; 

これも希望ユーザーが同じボタンを2回押して選択を取り消した場合に機能します。

希望すると、これが役立ちます。

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は、押されたボタンは、第1または第2のであれば制御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 beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.2]; 
     //set animation value means the next postion where u want to move object 
     [UIView commitAnimations]; 
のようなお願いしUIViewのアニメーションを実行する必要があります。

とにかく

は、あなたはあなたのすべての25個のボタンに一つだけの方法を使用することができますそれ

+0

これはうまいですが、iOS 4以降ではこれらの方法を使用することはお勧めしません。代わりにブロックベースのアニメーションメソッドを使用します。 – Beppe

0

tag.Thenストアを、これを達成するためのボタンを作成する際に、アレイ内のすべてのボタンを追加し、各ボタンを設定するための代替の両方のタグがbutton.Useを押下[buttonarray objectAtIndex:previous.tag]を使用してボタンを読み込み、前のボタンの現在のタグセットの背景イメージに設定する前に押されたタグ。

関連する問題