2009-06-22 48 views
3

點擊iPhone按鈕時是否可以製作自定義動畫?我喜歡App Store按鈕這樣的東西 - 它顯示價格,然後當你點擊它時,它會改變顏色和文本更改爲現在購買,然後當你再次點擊它時,它完成購買。iPhone - 如何按下時按動畫按鈕?

UIViewAnimationTransition只包含一些不提供完整功能的值。是否有可能做這樣的事情,但與動畫:

- (IBAction) changeButtonDisplay:(id)sender { 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.8]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:purchaseButton cache:NO]; //the built in UIViewAnimationTransition values don't provide enough flexibility 

    [purchaseButton setTitle:@"Buy Now" forState:UIControlStateNormal]; 
    [purchaseButton setBackgroundColor:[UIColor greenColor]]; 
    [purchaseButton addTarget:self action:@selector(purchaseItems:) forControlEvents:UIControlEventTouchUpInside]; 

    [UIView commitAnimations]; 

} 

此代碼的工作,並會顯示正確的過渡,並允許第二次點擊做適當的行動,但瞬間的標題和顏色的變化,而不是在流暢的動畫中。是否有可能輕鬆地做這樣的動畫,我是否必須創建一個UIButton子類並動畫「手動」?如果是這樣,我需要重寫什麼方法?

回答

4

並非只是更改按鈕的標題和目標等,而是有兩個單獨的按鈕可以執行每個操作並且看起來不同。然後當你按下按鈕時,調用一個方法從當前的超級視圖中移除當前按鈕,並將新按鈕添加到視圖中。把它放在一些UIView動畫塊中,你應該得到你的動畫。

此外,爲此,您需要在按鈕的超級視圖上設置動畫轉換。因此,可能有一個'容器視圖'可能很方便,然後將按鈕添加爲子視圖。

僞代碼可能看起來像這樣:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.8]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:buttonConteinerView cache:NO]; //the built in UIViewAnimationTransition values don't provide enough flexibility 

[purchaseButton retain]; 
[purchaseButton removeFromSuperView]; 

[buttonContainerView addSubview:secondPurchaseButton]; 

[UIView commitAnimations]; 
+1

此外,默認的動畫過渡是一個褪色,所以你可能不會需要設置的動畫過渡。 – Jasarien 2009-06-22 23:08:59

3

A標準的UIView會做你想要什麼。 UIView的backgroundColor是一個動畫屬性。

我會通過繼承UIView來創建自己的「按鈕」,或者替代子類UIButton併爲其添加子視圖。 (注:在UIButtons 3.0是一個關於這種疼痛在2.2,不知道)

你需要一個UILabel每個文本狀態:

UILabel *textForStateOne; 
UILabel *textForStateTwo; 

,你可以改變父視圖的背景顏色。

,那麼你在你的新按鈕類做的方法:

-(void)toggleState{ 

myState = !myState; 

    if(myState){ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.8]; 

     [textForStateOne setAlpha:1.0] 
     [textForStateTwo setAlpha:0.0] 

     [self setBackgroundColor:[UIColor greenColor]]; 

     [UIView commitAnimations]; 


    } else{ 

     //do the opposite here 

    } 




} 
0
UIView *btnView4=button; 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btnView4 cache:YES]; 
[UIView commitAnimations];`` 
+0

請學習如何格式化代碼(輸入框頂部有一個問號:-) – kleopatra 2013-08-05 10:19:38