[英]Animating UIButtons
我有幾個要設置動畫的按鈕。
我已經查看並嘗試了animateWithDuration方法,但這似乎是針對Views的,我無法使我的實現生效。
這種方法也可以為按鈕設置動畫嗎? 如果沒有,怎么辦?
編輯我有以下代碼
-(void) animate:(UIButton*) b withMonster: (int) monster withState: (int) state andLastState:(int) last_state {
if (state < last_state) {
float duration = 1.0 / 10;
__block int stateTemp = state;
[b animateWithDuration: duration
animations: ^{ [UIImage imageNamed:[NSString stringWithFormat:@"m%d.a000%d.png", monster, state]]; }
completion: ^{ [self animate: A1 withMonster: monster withState: stateTemp++ andLastState: last_state];; }];
}
我這樣稱呼它
[self animate: A1 withMonster: monsterDecider withState: 1 andLastState: 5];
當此代碼執行時,應用程序崩潰。
在兩個調用中都使用self調用是否正確(self是ViewController)。
謝謝
您可以為按鈕設置動畫,是的。 在動畫塊中,您需要更改一些按鈕參數,如下所示:
[UIView animateWithDuration:animTime
animations:^{button.frame = newFrame;}];
查看您的代碼,您可能希望將button.imageView.image屬性設置為新圖像(您的代碼當前加載了新圖像,但未將其設置到按鈕上)。
但是,UIImageView對象實際上支持在多個圖像之間進行更改,而無需使用UIView animateWithduration:...方法。
您可能要設置以下屬性:
button.imageView.animationImages
button.imageView.animationDuration
button.imageView.animationRepeatCount
然后打電話
[button.imageView startAnimating];
UIImageView將為您制作所有動畫。 請參閱UIImageView上的文檔: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.