2012-01-22 45 views
1

我有視圖動畫在Xcode中完美地工作(用CGRect按鈕單擊滑下屏幕)。不幸的是,我必須單擊按鈕兩次才能執行動畫。我做錯了什麼?我明白任何反饋按鈕觸發雙擊而不是單擊

這裏是我的.H代碼

@interface AnimationBlocksViewController : UIViewController{ 
    IBOutlet UIView *theview; 
    IBOutlet UIView *theview2; 

    BOOL isAnimated; 
    BOOL switchback; 
} 

-(IBAction)animate:(id)sender; 
-(IBAction)change:(id)sender; 

這裏是我的.M碼

-(IBAction)animate:(id)sender;{ 
    if (isAnimated) { 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview setFrame:CGRectMake(0, 0, 320, 460)]; 
     [theview2 setFrame:CGRectMake(320, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     isAnimated=NO; 

    } 


    else{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview setFrame:CGRectMake(-320, 0, 320, 460)]; 
     [theview2 setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     isAnimated=YES; 

    } 
} 

-(IBAction)change:(id)sender;{ 
    if (switchback) { 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview2 setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     switchback=NO; 

    } 


    else{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview2 setFrame:CGRectMake(320, 0, 320, 460)]; 
     [theview setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     switchback=YES; 

    } 
} 

它的工作原理,但我需要雙擊來觸發動畫,我做了什麼錯? 謝謝

回答

0

你最初雙擊後,做一個後續點擊觸發動畫是否正常?如果是這樣,可能是因爲您沒有明確初始化變量BOOL,但它們應該默認爲NO。嘗試這樣做,看看它是否有幫助。

如果你總是需要做兩次水龍頭動畫,也許嘗試插入一個調用運行循環,看看它是否只處理動畫後,你的應用程序線程上運行了一段時間,即它可能是,只有當它來處理第二個點擊事件是否實際發送並獲取任何待處理的動畫並執行它們。該代碼是(你設置isAnimated並調用commitAnimations後):

[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate date]]; 

不知道你是否可以只使用nil的日期,但嘗試,太多,如果你喜歡。

+0

嗯讓我改變這些動畫狀態,看看會發生什麼。該按鈕目前僅通過雙擊動畫觀看 –

+0

YES!有效!更改我的按鈕的動畫狀態解決了問題,謝謝darvidsOn –

+0

(我將所有動畫狀態更改爲NO –