[英]UILabel animating number change
我有一個顯示用戶分數的UILabel。 並且評分會不時變化,是否有辦法對此更改進行動畫處理,以便將此數字從其當前值緩慢增加到其結果值? 像http://josheinstein.com/blog/index.php/2010/02/silverlight-animated-turbotax-number-display/這樣的東西,但是對於objective-c。
使用CADisplayLink在一段時間內更改UILabel的自定義子類的文本屬性。 您可能希望使用NSNumberFormatter
來獲得更漂亮的輸出。
// Create instance variables/properties for: `from`, `to`, and `startTime` (also include the QuartzCore framework in your project)
- (void)animateFrom:(NSNumber *)aFrom toNumber:(NSNumber *)aTo {
self.from = aFrom; // or from = [aFrom retain] if your not using @properties
self.to = aTo; // ditto
self.text = [from stringValue];
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(animateNumber:)];
startTime = CACurrentMediaTime();
[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)animateNumber:(CADisplayLink *)link {
static float DURATION = 1.0;
float dt = ([link timestamp] - startTime) / DURATION;
if (dt >= 1.0) {
self.text = [to stringValue];
[link removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
return;
}
float current = ([to floatValue] - [from floatValue]) * dt + [from floatValue];
self.text = [NSString stringWithFormat:@"%i", (long)current];
}
AUIAnimatedText
擁有您所要求的一切。 這是UILabel的替代,具有觸及文本動畫的可能性,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.