2012-07-25 72 views
3

我仍然在學習UIAnimations,剛剛進入它,我都在,我不知道如何解決問題絆倒了。我看過你獲得新高分的比賽,並且將新的高分添加到舊的高分中,並且他們讓數字呈現出上下的動態。它看起來非常酷,視覺上安撫。動畫的UILabel與數字

任何人都可以向我解釋這是怎麼完成的?我很抱歉,如果這個問題很容易解決,就像我說我仍然在學習/完美的動畫。

在此先感謝

回答

9

我從post sergio suggested you look at那裏得到了代碼,但是注意到安舒提到你想要上下動畫而不是淡入/淡出動畫,所以我改變了代碼以符合你的想法。在這裏你去:

// Add transition (must be called after myLabel has been displayed) 
CATransition *animation = [CATransition animation]; 
animation.duration = 1.0; //You can change this to any other duration 
animation.type = kCATransitionMoveIn;  //I would assume this is what you want because you want to "animate up or down" 
animation.subtype = kCATransitionFromTop; //You can change this to kCATransitionFromBottom, kCATransitionFromLeft, or kCATransitionFromRight 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
[myLabel.layer addAnimation:animation forKey:@"changeTextTransition"]; 

// Change the text 
myLabel.text = newText; 

希望這會有所幫助!

+0

非常感謝!將接受 – 2012-07-25 23:45:24

+0

@AlexG - 沒問題。您可能需要稍微改動一下參數,但應該可以正常工作。 – pasawaya 2012-07-25 23:46:00

2

人們可以糾正我,如果我錯了這裏,但我敢肯定,你必須手工編碼這個動畫。如果你足夠努力,你可能會在網上找到一個開源版本。

也許可以拍攝UILabel的圖像,並使用sizeWithFont:來確定每個字符的寬度,然後根據每個數字將圖像切分爲多個部分。或者,您可以爲每個數字設置多個UILabel

一旦你有數字圖像的數組,你必須計算該數字將會在過渡期間,以及他們是否打算增加或減少,然後通過從推過渡到下一個數字改變頂部/底部(我認爲這是一個內置的轉換,可以在Core Animation文檔中查看)。

你可能會想他們是多麼增加/減少,並用它來計算出動畫需要多長時間才能確定。這樣一來,如果你打算從5到900,最後一個數字將不得不很快動畫,倒數第二個會盡快動畫1/10,第三個是1/100等

+0

感謝您的解釋。 Qegal提供了這樣的例子,只是因爲這個原因我才接受。 +1雖然很好的回答 – 2012-07-25 23:46:03