zoukankan      html  css  js  c++  java
  • 动态显示数字

    //类似于支付宝余额的动态显示
    @property (nonatomic, assign) float balance;

    - (void)dealloc {

    
    
    
    
    

        //释放定时器

    
    

        [_balanceLabelAnimationTimer invalidate];

    
    

         _balanceLabelAnimationTimer = nil;

    
    

    }

    - (void)setBalance:(float)balance {
        
        _balance = balance;
    
        [self setNumberTextOfLabel:self.moneyLab WithAnimationForValueContent:balance];
      
        
    }
    
    #pragma mark --- 余额支付的动画----
    - (void)setNumberTextOfLabel:(UILabel *)label WithAnimationForValueContent:(CGFloat)value
    {
        CGFloat lastValue = [label.text floatValue];
        CGFloat delta = value - lastValue;
        if (delta == 0) {
       
            label.text = @"0.00";
            return;
        }
       
        if (delta > 0) {
            
            CGFloat ratio = value / 60.0;
            
            NSDictionary *userInfo = @{@"label" : label,
                                       @"value" : @(value),
                                       @"ratio" : @(ratio)
                                       };
            _balanceLabelAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(setupLabel:) userInfo:userInfo repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:_balanceLabelAnimationTimer forMode:NSRunLoopCommonModes];
        }
    }
    
    - (void)setupLabel:(NSTimer *)timer
    {
        NSDictionary *userInfo = timer.userInfo;
        UILabel *label = userInfo[@"label"];
        CGFloat value = [userInfo[@"value"] floatValue];
        CGFloat ratio = [userInfo[@"ratio"] floatValue];
        
        static int flag = 1;
        CGFloat lastValue = [label.text floatValue];
        CGFloat randomDelta = (arc4random_uniform(2) + 1) * ratio;
        CGFloat resValue = lastValue + randomDelta;
        
        if ((resValue >= value) || (flag == 50)) {
            label.text = [NSString stringWithFormat:@"%.2f", value];
            flag = 1;
            [timer invalidate];
            timer = nil;
            return;
        } else {
            label.text = [NSString stringWithFormat:@"%.2f", resValue];
        }
        
        flag++;
        
    }
  • 相关阅读:
    CentOS5.6下SVN的安装
    在servlet中的init方法中使用getInitParameter方法空指针错误
    Linux iostat监测IO状态【转】
    自己实现一个list比较器 实现Comparator()接口
    一些常用的随机实现
    java里null强转为某个类会报错吗?
    java游戏服务器简单工厂模式
    起个头!准备写一个 设计模式系列
    HashMap根据value值排序
    java游戏服务器 策略+简单工厂
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5825509.html
Copyright © 2011-2022 走看看