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

细节

时间: 2024-08-04 23:49:09

POP数值动画的相关文章

iOS利用Runtime自定义控制器POP手势动画

前言 苹果在iOS 7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操作. nav_pop_origin.gif 这个操作的想法非常好,但是系统给我们规定的范围必须是屏幕左侧边缘才可以触发,这样实际使用过程中对于有些产品会产生不便,于是有些app就采取整个屏幕都响应这个手势并且pop动画还是用系统原生的,这样操作起来确实方便好多. nav_pop_custom.gif

Runtime__iOS利用Runtime自定义控制器POP手势动画

前言 苹果在IOS7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操作. nav_pop_origin.gif 这个操作的想法非常好,但是系统给我们规定的范围必须是屏幕左侧边缘才可以触发,这样实际使用过程中对于有些产品会产生不便,于是有些app就采取整个屏幕都响应这个手势并且pop动画还是用系统原生的,这样操作起来确实方便好多. nav_pop_custom.gif

iOS利用Runtime自定义控制器POP手势动画(经典)

前言 苹果在IOS7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操作. nav_pop_origin.gif 这个操作的想法非常好,但是系统给我们规定的范围必须是屏幕左侧边缘才可以触发,这样实际使用过程中对于有些产品会产生不便,于是有些app就采取整个屏幕都响应这个手势并且pop动画还是用系统原生的,这样操作起来确实方便好多. nav_pop_custom.gif

POP按钮动画

效果 源码 https://github.com/YouXianMing/Animations // // ButtonPressViewController.m // Facebook-POP-Animation // // Created by YouXianMing on 15/11/16. // Copyright © 2015年 ZiPeiYi. All rights reserved. // #import "ButtonPressViewController.h" #im

POP简单动画简单使用 (入门级别)

动画可以让APP"更友好"的与用户交互,苹果提供很多的好看的动画供开发者使用,不过简单的平移.旋转.缩放.......使用起来很简单,但是想要进行一些比较复杂的动画效果,使用起来就比较难以实现,俗话说需求促进开发,facebook提供的开源的框架供我们免费使用,底层使用的是c++去实现,流畅度不输苹果的动画效果,甚至有的动画效果看起来比苹果提供的动画更"流畅"...... 这篇文章介绍大名鼎鼎的POP动画的使用 POP:   https://github.com/f

POP缩放动画

效果 源码 https://github.com/YouXianMing/Animations // // SpringScaleViewController.m // Animations // // Created by YouXianMing on 16/6/3. // Copyright © 2016年 YouXianMing. All rights reserved. // #import "SpringScaleViewController.h" #import "

OC UINavigationController push pop自定义动画

1. DSLTransitionFromFirstToSecond #import <UIKit/UIKit.h> @interface DSLTransitionFromFirstToSecond : NSObject<UIViewControllerAnimatedTransitioning> /** isPresenting是否是pop动画, */ @property (nonatomic, assign) BOOL isPresenting; @end #import &q

ios pop 折叠动画

今天写了个挺有意思得动画,大家可以去githoub上下载.这个动画效果也是一位前辈写的,我参考了它,并有注释,希望大家都能看的懂.各种动画,的确是有些难懂刚开始,我也才刚摸索中.希望能够多多交流.githoub链接  https://github.com/wang820203420/poptop.

下拉弹窗 pop push动画实现

- (void)popTitleView:(UIButton *)btn { if (popView.superview ==self.view) { CATransition *animation =[CATransition animation]; animation.delegate=self; animation.duration=0.3; animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaT