Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UILabel动画编号更改_Iphone_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Iphone UILabel动画编号更改

Iphone UILabel动画编号更改,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,我有一个显示用户分数的UILabel。随着分数的不断变化,有没有一种方法可以使这个变化变得生动,慢慢地将这个数字从它的当前值增加到它的结果值? 类似于但对于objective-c.AUIAnimatedText的内容满足了您的所有要求。这是用reach文本动画功能替代UILabel,使用CADisplayLink在一段时间内更改UILabel自定义子类的文本属性。您可能希望使用NSNumberFormatter获得更漂亮的输出 // Create instance variables/prope

我有一个显示用户分数的UILabel。随着分数的不断变化,有没有一种方法可以使这个变化变得生动,慢慢地将这个数字从它的当前值增加到它的结果值?
类似于但对于objective-c.

AUIAnimatedText
的内容满足了您的所有要求。这是用reach文本动画功能替代UILabel,使用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];
}

我发现这可能会有帮助首先查找答案是的,我看到了,不完全是我要找的动画。注意,如果您确实使用
NSNumberFormatter
,请创建其中一个,并在使用相同格式的每个格式化作业中重复使用它。创建一个新的
NSNumberFormatter
非常昂贵,但是重新使用一个现有的格式很便宜。我一直在
float current=([to floatValue]-[from floatValue])*dt+[from floatValue]
中获得EXC_BAD_access;您是否为
from
to
变量使用属性?它是否设置为
retain
?如果您不使用properties do
from=[aFrom retain]
它在模拟器上非常有效,但在iphone上却不行。它显示1个介于“从”和“到”之间的数字,然后显示“到”数字。似乎它落后了。有什么建议吗?在模拟器和设备上都很有用。真棒,杰森。你太棒了!