簡體   English   中英

UILabel動畫編號更改

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM