Ios 增量数字动画
我有一个显示收入和支出价值的主页。现在这些值只是出现在屏幕上,但是我想知道是否可以让这些数字从0开始,然后用动画来增加该值,就像秒表一样。是否也可以在增量完成后制作最终数字,以使用脉冲进行动画制作”。这就是我现在制作标签的方式,但标签从左侧变大,而不是从中心变大Ios 增量数字动画,ios,objective-c,animation,Ios,Objective C,Animation,我有一个显示收入和支出价值的主页。现在这些值只是出现在屏幕上,但是我想知道是否可以让这些数字从0开始,然后用动画来增加该值,就像秒表一样。是否也可以在增量完成后制作最终数字,以使用脉冲进行动画制作”。这就是我现在制作标签的方式,但标签从左侧变大,而不是从中心变大 [self.view addSubview:_lblTotal]; CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"tra
[self.view addSubview:_lblTotal];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration = 0.4;
scaleAnimation.repeatCount = HUGE_VAL;
scaleAnimation.autoreverses = YES;
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.8];
scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
[_lblTotal.layer addAnimation:scaleAnimation forKey:@"scale"];
是的,这是可能的
添加一个带有要递增的数字的变量
@property (nonatomic) double currentValue;
@property (nonatomic, retain) NSTimer *timer;
在viewDidLoad中初始化它
_currentValue = 0.00;
编写一个函数updateNumber
-(void)updateNumber {
_currentValue = _currentValue + 0.01;
[_label setText:[NSString stringWithFormat:@"%.2f", _currentValue]];
if(_currentValue >= 100.0) { //example to stop incrementation on 100
_timer = nil; // stop the timer
// do your animation
}
}
在您的视图中显示()
if(_timer == nil) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 //seconds
target:self
selector:@selector(updateNumber)
userInfo:nil
repeats:true];
}
您可能希望将递增的数字和脉冲动画分为两个不同的问题
关于脉冲的问题,代码是正确的,但它偏离中心的原因可能是由于视图的框架比其内容大,它看起来是右对齐的,而不是居中的
如果您通过调用[[u lblTotal sizeToFit];
使[u lblTotal sizeToFit]使[u lblTotal sizeToFit]的框架适合内容,则您的脉冲动画应该按预期工作。但是,这假设您自己进行了正确的对齐。以下开源UILabel子类不支持您要查找的滚动数字位
使用类型的示例如下:
myLabel.method = UILabelCountingMethodLinear;
[myLabel countFrom:50 to:100 withDuration:5.0f];
我想看看这是如何完成的,然后扩展它,在计数动画完成后在末尾添加脉冲。下面是一个示例代码,它肯定会帮助您,但您必须自定义计时,因为它肯定适合我的需要:-
我该如何处理十进制数?我希望计数器在达到某一特定值时停止,例如图中的338.00question@John有一点自我想象是必要的;)我更新了我的答案,以包括小数。只需使用浮点或双精度而不是整数。我尝试过这种方法,但项目崩溃了。说un已识别的选择器已发送到实例。您知道为什么会发生这种情况吗?没有源代码是不行的。不要只是复制/粘贴我的代码。尝试理解它并使其适应您的个人项目。如果您只是复制/粘贴解决方案,您将不会学到任何东西;)请在@selector(updateNumber)上尝试使用:
我在计时器中错误地键入了updateNumber,因此我成功地修复了该错误。我有些理解它的工作原理,但我对数字不递增的原因感到困惑。当我重复更改时:否为是,标签将更新,但当否为否时,标签将立即更改为0.01,然后停止。这是因为它没有调用updateNumber方法吗又是d?为什么是-1。这个链接的代码完全符合问题的要求。这是一个包含数字(int或float)的标签的动画从开始到结束都有一个给定的持续时间和动画风格!我没有投反对票,但我推测投反对票是因为如果该项目结束,这个答案将毫无用处。答案应该提供一些有用的信息事件,没有外部链接。链接应该补充答案,而不是唯一的内容。另外,。使用UI“非Apple类上的代码>前缀特别离谱。@robmayoff理解。没有时间在链接处拆除代码,但按要求发布。至于字母UI的使用,而不是我的代码8^)。你可以将链接作为问题的注释发布。嗨,我尝试过这种方法,我的项目崩溃了。知道为什么吗?unreco?”gnized selector发送到instanceWhich line它是crashingI刚刚自定义了我代码的一小部分,以使您理解。否则,从我的角度来看,它工作得很好。@John
-(void)viewDidLoad
{
currentValueAmount=0; //provide initial value for your incremented label
targetFloatAmount=1300;//provide the target value up-to which the value will be incremented.
self.lblAmount.text=[NSString stringWithFormat:@"%.2f",currentValueAmount];
[self performSelector:@selector(animateIncrementLabel) withObject:nil afterDelay:0.5f]; // give some delay as per your choice as to load the whole UI first and then calling the method to increment.
}
-(void)animateIncrementLabel
{
self.lblAmount.transform=CGAffineTransformMakeScale(0.000f, 0.000f);
timer = [NSTimer scheduledTimerWithTimeInterval: 0.001 //calling timer , if the label value is very small then increase the no of Zero's for example 0.00001
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES];
//animating the incremented text with some transform and bounce
[UIView animateWithDuration:2.1 delay:0.2f usingSpringWithDamping:0.4 initialSpringVelocity:0.3 options:0 animations:^{
self.lblAmount.transform=CGAffineTransformMakeScale(1.3, 1.3);
}];
}
- (void) handleTimer: (NSTimer *) timer
{
if(currentValueAmount>targetFloatAmount)
{
self.lblAmount.text=[NSString stringWithFormat:@"%.2f",targetFloatAmount];
[timer invalidate];
timer=nil;
[UIView animateWithDuration:0.5 delay:0.2f usingSpringWithDamping:0.4 initialSpringVelocity:0.3 options:0 animations:^{
self.lblAmount.transform=CGAffineTransformMakeScale(1.0, 1.0);
}];
}
else
{
currentValueAmount=currentValueAmount+0.0008;
self.lblAmount.text=[NSString stringWithFormat:@"%.2f",currentValueAmount];
}
}