0

我想为向上计算的文本中的数字计数设置动画。文本位于类似的 UILabelYou have driven for 0.0km中,需要更改为You have driven for 143.6km带有计数动画。有什么办法可以更新动画吗?

编辑 这是我当前的一些代码,关于我已经拥有的其他动画:

        if (animated)
        {
            [UIView beginAnimations:@"scaleAnimation" context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDuration:animationDuration];
        }

        [...]

        // Amount pointer
        float xForRedBar = redBarFrame.size.width + redBarFrame.origin.x;

        CGRect pointerFrame = cell.amountBarPointer.frame;
        pointerFrame.origin.x = (xForRedBar - (pointerFrame.size.width/2));

        if (pointerFrame.origin.x < 12)
            pointerFrame.origin.x = 12;

        if (pointerFrame.origin.x >= (308 - (pointerFrame.size.width/2)))
            pointerFrame.origin.x = 308 - pointerFrame.size.width;

        [cell.amountBarPointer setFrame:pointerFrame];

        // Amount bar
        CGRect amountBarFrame = cell.amountBar.frame;
        amountBarFrame.origin.x = 9+(((302 - amountBarFrame.size.width)/100)*self.procentCompleted);

        [cell.amountBar setFrame:amountBarFrame];

        // Amount info text
        CGRect amountInfoFrame = cell.amountInfo.frame;
        amountInfoFrame.origin.x = amountBarFrame.origin.x + 2;

        [cell.amountInfo setFrame:amountInfoFrame];

        // Amount text
        [cell.amountInfo setText:[NSString stringWithFormat:NSLocalizedString(@"You have driven for %@km", nil), self.userAmount]];

        [...]

        if (self.procentCompleted == 0)
        {
            [cell.amountBar setAlpha:0];
            [cell.amountBarPointer setAlpha:0];
            [cell.amountInfo setAlpha:0];
        }
        else {
            [cell.amountBar setAlpha:1];
            [cell.amountBarPointer setAlpha:1];
            [cell.amountInfo setAlpha:1];
        }

        if (animated)
        {
            [UIView commitAnimations];
        }
4

1 回答 1

1

当然,您可以将其更新为“动画”,但您必须使用重复计时器,并在每次调用计时器的选择器时将一个(或您想要的任何间隔)添加到计数中。包括对最终值的测试,并在您到达那里时使计时器无效。

编辑后:

如果您希望计数的速度在最后减慢,我不会使用计时器,我会使用 performSelector:withObject:afterDelay:。为了让它重复,你可以从它的选择器中调用这个方法。您需要进行一些测试,看看您是否接近计数结束,然后在每次通过时增加一点延迟时间。像这样的东西:

-(IBAction)countUp:(id)sender {
    [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1];
}

-(void)countUpLabel {
    static float delay = .01;
    num+= 1;
    label.text = [NSString stringWithFormat:@"%d",num];
    if (num < 40) {
        [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1];
    }else if (num > 35 && num <50) {
        [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1 + delay];
        delay += 0.01;
    }
}
于 2013-02-12T15:40:39.263 回答