POP数值动画

简介:

POP数值动画

 

效果

 

 

源码

https://github.com/YouXianMing/Animations

//
//  PopNumberController.m
//  Animations
//
//  Created by YouXianMing on 15/11/18.
//  Copyright © 2015年 YouXianMing. All rights reserved.
//

#import "PopNumberController.h"
#import "POPNumberAnimation.h"
#import "GCD.h"
#import "StringAttributeHelper.h"

@interface PopNumberController () <POPNumberAnimationDelegate>

@property (nonatomic, strong) POPNumberAnimation *numberAnimation;
@property (nonatomic, strong) GCDTimer           *timer;
@property (nonatomic, strong) UILabel            *label;

@end

@implementation PopNumberController

- (void)viewDidLoad {
    
    [super viewDidLoad];
}

- (void)setup {

    [super setup];
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.center        = self.view.center;
    [self.view addSubview:_label];
    
    // Init numberAnimation.
    self.numberAnimation          = [[POPNumberAnimation alloc] init];
    self.numberAnimation.delegate = self;
    
    // Timer event.
    __weak PopNumberController *weakSelf = self;
    self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [self.timer event:^{
        
        // Start animation.
        [weakSelf configNumberAnimation];
        [weakSelf.numberAnimation startAnimation];
        
    } timeIntervalWithSecs:3.f];
    [self.timer start];
}

- (void)configNumberAnimation {

    self.numberAnimation.fromValue      = self.numberAnimation.currentValue;
    self.numberAnimation.toValue        = (arc4random() % 101 / 1.f);
    self.numberAnimation.duration       = 2.f;
    self.numberAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.69 :0.11 :0.32 :0.88];
    [self.numberAnimation saveValues];
}

- (void)POPNumberAnimation:(POPNumberAnimation *)numberAnimation currentValue:(CGFloat)currentValue {
    
    // Init string.
    NSString *numberString = [NSString stringWithFormat:@"%.1f", currentValue];
    NSString *mpsString    = @"mps";
    NSString *totalString  = [NSString stringWithFormat:@"%@ %@", numberString, mpsString];
    
    // Init string ranges.
    NSRange   mpsRange     = [totalString rangeOfString:mpsString];
    NSRange   numberRange  = [totalString rangeOfString:numberString];
    NSRange   totalRange   = NSMakeRange(0, totalString.length);
    
    // Init attributes.
    FontAttribute *totalFont = [FontAttribute new];
    totalFont.font           = Font_Avenir_Light(20.f);
    totalFont.effectRange    = totalRange;
    
    FontAttribute *numberFont = [FontAttribute new];
    numberFont.font           = Font_HYQiHei(60.f);
    numberFont.effectRange    = numberRange;
    
    ForegroundColorAttribute *totalColor = [ForegroundColorAttribute new];
    totalColor.color                     = [UIColor blackColor];
    totalColor.effectRange               = totalRange;

    ForegroundColorAttribute *mpsColor   = [ForegroundColorAttribute new];
    mpsColor.color                       = [self mpsColorWithValue:currentValue / 100.f];
    mpsColor.effectRange                 = mpsRange;
    
    ForegroundColorAttribute *numColor   = [ForegroundColorAttribute new];
    numColor.color                       = [self numColorWithValue:currentValue / 100.f];
    numColor.effectRange                 = numberRange;
    
    // Create richString.
    NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:totalString];
    [richString addStringAttribute:totalFont];
    [richString addStringAttribute:totalColor];
    [richString addStringAttribute:numberFont];
    [richString addStringAttribute:mpsColor];
    [richString addStringAttribute:numColor];
    
    _label.attributedText = richString;
}

- (UIColor *)numColorWithValue:(CGFloat)value {

    return [UIColor colorWithRed:value green:0 blue:0 alpha:1.f];
}

- (UIColor *)mpsColorWithValue:(CGFloat)value {
    
    return [UIColor colorWithRed:0 green:value / 2.f blue:value / 3.f alpha:value];
}

@end

细节

目录
相关文章
|
JavaScript 数据处理
👻关于ECharts的那些事儿(数据超出Y轴最大值max又不想它隐藏)
👻关于ECharts的那些事儿(数据超出Y轴最大值max又不想它隐藏)
1189 0
数组求最大值和最小值的方法,数组写最大值和最小值的三元写法,数组取最小值,如何在数组中添加最后一个值,arr.push()添加数组放到后面,arr.unshift(‘red‘)添加的放到前面
数组求最大值和最小值的方法,数组写最大值和最小值的三元写法,数组取最小值,如何在数组中添加最后一个值,arr.push()添加数组放到后面,arr.unshift(‘red‘)添加的放到前面
求出N×M整型数组的最大元素及其所在的行坐标及列坐标(如果最大元素不唯一,选择位置在最前面的一个)。
求出N×M整型数组的最大元素及其所在的行坐标及列坐标(如果最大元素不唯一,选择位置在最前面的一个)。
328 0
|
前端开发 JavaScript 中间件
关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧!
关于浮动元素,你还在自己计算位置吗?来看看 Floating UI 吧!
258 0
给定一个数组nums和一个值val,你需要原地移除所有数值等于val的元素,并返回移除后数组的新长度?
给定一个数组nums和一个值val,你需要原地移除所有数值等于val的元素,并返回移除后数组的新长度?
314 0
给定一个数组nums和一个值val,你需要原地移除所有数值等于val的元素,并返回移除后数组的新长度?
把一个数组的元素全部显示在另外一个数组里面;未赋初始值
把一个数组的元素全部显示在另外一个数组里面;未赋初始值
83 0
把一个数组的元素全部显示在另外一个数组里面;未赋初始值
shader中判断浮点型数值相等:round、floor
shader中判断浮点型数值相等:round、floor
319 0
编写一个满足下面要求的程序:创建一个由100个随机选取的整数构成的数组 提示用户输入数组的下标,然后显示对应的元素值,如果指定的下标越界,则显示消息“Out of Bounds”
编写一个满足下面要求的程序:创建一个由100个随机选取的整数构成的数组 提示用户输入数组的下标,然后显示对应的元素值,如果指定的下标越界,则显示消息“Out of Bounds”
172 0
|
JavaScript
float元素先后顺序
float元素先后顺序
float元素先后顺序
Echarts折线图line-stack数值和数值显示不一致
Echarts折线图line-stack数值和数值显示不一致
250 0
Echarts折线图line-stack数值和数值显示不一致