2012-11-18 34 views
1

我有一個UIButton設置在故事板上,當按下動畫UIVIew時。我希望同一個按鈕在第二次按下時將UIView動畫/移動回原來的位置。UIButton動作

這樣做的最好方法是什麼?

感謝您的任何幫助或指針。

這是我使用動畫和移動的UIView代碼:

我使用了一個名爲BOOL buttonCurrentStatus。

-(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"); 

    } 
+1

這是不夠的CGRect伊娃保持最後一幀? – Mat

+0

我不明白。你有一個例子,它的設置正確嗎?我非常有興趣學習如何在第一次觸摸時執行一個動作,然後在第二次觸摸時執行不同的動作。謝謝 – hanumanDev

+1

我的意思是你可以使用一個CGRect類變量臨時存儲最後一幀。然後,當您按下按鈕時,只需將按鈕位置重置爲使用的最後一幀。否則,如果結束幀始終相同,請根據需要重新設置sender.frame並重置幀。 (對不起,我在iPad上,如果沒有人回答你,我會盡快完成)。 – Mat

回答

1

另一種更普遍的方法是使用,將執行以下兩種方法之一的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
-(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]; 
} 
1

非常簡單地解決你的問題是,如果你在聲明類中的兩個布爾變量,你需要實現這個method.your代碼將看起來像這樣

在.m文件

BOOL Action1; 
BOOL Action2; 

現在在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; 
    } 

希望它對你有用。謝謝

+0

這很棒。謝謝! – hanumanDev