2012-11-07 73 views
3

我有一個白色圖像按鈕。當我點擊時,它應該通過從底部到頂部的動畫變成黃色按鈕圖像,如果我再次單擊黃色按鈕圖像,它必須從上到下進行動畫,並更改爲白色圖像按鈕圖像。帶動畫的UIButton圖像按鈕

這是我的嘗試:

float originalY = btn.frame.origin.y; 
    float originalH = btn.bounds.size.height; 

    [UIView animateWithDuration:3.0f delay:1.0f  options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{ 

    btn.frame = CGRectMake(btn.frame.origin.x, (originalY + originalH), btn.bounds.size.width, 0); 

    [btn setBackgroundImage:[UIImage imageNamed:@"yellowimg.png"] forState:UIControlStateNormal]; 

    [btn setTitleColor:[[UIColor alloc]initWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal]; 

    }  
    completion:^(BOOL finished) { 
     }]; 
+0

@Prateek:我曾嘗試與一個我更新,實際上我需要兩個按鈕圖像之間的動畫。 – Preethi

回答

1
btn.frame = CGRectMake(btn.frame.origin.x, (originalY + originalH), btn.bounds.size.width, 0); 

這種方法會讓你的按鈕,height = 0。 如果你想捲曲(翻轉)您的按鈕,並有「新」按鈕,從第一次出現,因爲我知道你應該使用類似的東西

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstView cache:YES]; 
[firstView addSubview:secondView]; 
[UIView commitAnimations]; 
+0

從iOS4起推薦基於塊的動畫。 – Abizern

+0

@ x401om:我需要在按鈕圖像之間從下到上的過渡,並且在動畫時不要捲曲/按下按鈕。有什麼關係嗎? – Preethi