2013-02-12 74 views
0

我想动画计算向上计算digets的计数。文本是类似于You have driven for 0.0km的UILabel,需要使用计数动画将其更改为You have driven for 143.6km。有什么办法可以更新动画?更新UILabel文本动画

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

 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]; 
     } 
+0

在这里你去:http://stackoverflow.com/a/6267259/1228534 – graver 2013-02-12 15:01:20

+0

谢谢...但是,这并没有解决我的问题。 – 2013-02-12 15:07:53

+0

那么你的问题是如何改变每个'x'毫秒的数字,直到它从值'a'变为值'b'?答案就是使用'NSTimer',就像下面的@rdelmar建议的那样。或者你的问题与任何两个数字之间的变化的实际动画有关吗? – Mathew 2013-02-12 17:18:16

回答

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; 
    } 
} 
+0

听起来不那么有效,但我理解你的建议。也许不值得拥有“很高兴”的功能。 – 2013-02-13 09:16:38

+0

@PaulPeelen,我不认为这是特别的内存密集型,而且它是唯一能看到数字增加的方法(如果这就是你想要的)。我不确定“宽松”是什么意思 - 你希望它慢下来,因为它接近尾声? – rdelmar 2013-02-13 16:04:26

+0

正确。我希望它在开始时加快速度,并尽可能减慢速度。与'UIViewAnimationCurveEaseInOut'相同。 – 2013-02-14 08:20:56