增量数字动画

时间:2015-05-12 13:13:05

标签: ios objective-c animation

我有一个显示收入和支出价值的主页。现在这些值只是出现在屏幕上,但是想知道我是否可以让这些数字从0开始然后有一个动画来增加该值,就像秒表一样。是否也可以在增量完成后使用脉冲动画制作最终数字“。这就是我现在制作标签脉动的方法,但标签从左边开始变大而不是从中心开始。

[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"];

homepage

4 个答案:

答案 0 :(得分:2)

以下开源UILabel子类执行您要查找的滚动数字位。

https://github.com/dataxpress/UICountingLabel

使用类型的示例是:

list6 = set(['A', 'B', 'D', 'H', 'G'])
list10 = [set(['A', 'D', 'J']),
          set(['A', 'D', 'H'])]

def myfunc():
    for i in list10:
        if i.issubset(list6):
            return True
    return False

我会看看如何完成,然后扩展它以在计数动画完成后在末尾添加脉冲。

答案 1 :(得分:0)

是的,这是可能的。

添加一个带有要递增的数字的变量

@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
    }

}

在你的viewDidAppear()中,或者只要你想像这样开始计算

if(_timer == nil) {
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0  //seconds
            target:self
            selector:@selector(updateNumber)
            userInfo:nil
            repeats:true];
}

答案 2 :(得分:0)

您可能希望将递增数字和脉冲动画分成两个不同的问题。

关于脉冲的问题,代码是正确的,但它偏离中心的原因可能是由于你的视图的帧大于其内容,它看起来是右对齐而不是居中。

如果您使_lblTotal的框架符合内容,则通过调用[_lblTotal sizeToFit];您的脉冲动画应按预期工作。但是,这假设你自己做了正确的对齐。

答案 3 :(得分:0)

这是一个示例代码,它肯定会对您有所帮助,但您必须自定义时间,因为它确实符合我的需要: -

-(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];



    }
}