我想在计算向上的文本中设置digets的计数动画。文本与UILabel类似,如You 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];
}
答案 0 :(得分: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;
}
}