1

ストーリーボードに UIButton を設定しました。これを押すと、UIVIew がアニメーション化されます。同じボタンを2回目に押したときに、UIViewを元の位置にアニメーション化/移動したいと思います。

これを行う最善の方法は何ですか?

助けや指針をありがとう。

これは、UIVIew をアニメーション化して移動するために使用しているコードです。

buttonCurrentStatus という名前の BOOL を使用しています。

-(IBAction)sortDeals:(UIButton*)sender {

if (buttonCurrentStatus == NO)
{
    buttonCurrentStatus = YES;

CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;


[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

self.menuTop.frame = menuTopFrame;

[UIView commitAnimations];
    NSLog(@"Sort Deals touched button status = yes");


} else {
        buttonCurrentStatus = NO;

        CGRect menuTopFrame = self.menuTop.frame;
        menuTopFrame.origin.y = 30;


        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

        self.menuTop.frame = menuTopFrame;

        [UIView commitAnimations];


NSLog(@"Sort Deals touched button status = no");

    }
4

3 に答える 3

1

もう1つのより一般的な方法は、2つのメソッドのいずれかを実行するIBActionを使用することです。前の回答と同様に、使用するアクションを決定するためにBOOl値またはintを使用できます。これはより一般的な答えであり、多くの目的に使用できます。

まず、BOOL変数が必要になります。この例では、Bool変数をboolVaribleとして配置しました(はい、スペルが間違っていることはわかっています)。デフォルトでは、YESに設定されています。

bool boolVarible = YES;

それをクラス変数にしました。

-(IBAction)buttonAction:(id)sender {
    if (boolVarible == YES)
    {
        //Execute first action
        [self firstAction];
    }
    else
    {
        //Execute second action
        [self secondAction];
    }
}

-(void)firstAction {
    //Do something here...
}

-(void)secondAction {
    //Do something here...
}

うまくいけば、あなたはアイデアを得るでしょう。その後、いつでもアクションを交換できます。BOOl値を変更するだけで、ボタンを押すと別のメソッドが実行されます。これは、すべてを1つのアクションで実行するよりもクリーンだと思います。幸運を。

于 2012-11-18T17:58:07.747 に答える
1
-(IBAction)sortDeals:(UIButton*)sender {

sender.selected = !sender.selected;

int moveby = (sender.selected) ? 30: -30;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
menuTop.frame = CGRectMake(menuTop.frame.origin.x, menuTop.frame.origin.y + moveby, menuTop.frame.size.width, menuTop.frame.size.height);
[UIView commitAnimations];
}
于 2012-11-18T15:17:04.007 に答える
1

あなたの質問に対する非常に簡単な解決策は、このメソッドを実装する必要があるクラスで2つのブール変数を宣言する場合です。コードは次のようになります

.m ファイル内

BOOL Action1;
BOOL Action2;

Now In ViewDidload メソッド

 (void)viewDidLoad 
 {
   Action1=TRUE;
   Action2=FALSE;
   [super viewDidLoad];

  }

そして今、あなたの方法

  (IBAction)sortDeals:(UIButton*)sender {
   if (Action1 == TRUE)
 {

   CGRect menuTopFrame = self.menuTop.frame;
   menuTopFrame.origin.y = 30;
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:0.5];
   [UIView setAnimationDelay:0.0];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
   self.menuTop.frame = menuTopFrame;
   [UIView commitAnimations];
   NSLog(@"Sort Deals touched button status = yes");
     Action1=FALSE;
     Action2=TRUE;
    }
   else 
    if (Action2 == TRUE)
   {

   CGRect menuTopFrame = self.menuTop.frame;
    menuTopFrame.origin.y = 30;
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.menuTop.frame = menuTopFrame;
   [UIView commitAnimations];
   NSLog(@"Sort Deals touched button status = no");
   Action1=TRUE;
   Action2=FALSE;
   }

それがあなたのために働くことを願っています.Thanks

于 2012-11-18T19:00:20.810 に答える