JTNumberScrollAnimatedView

本人视频教程系类   iOS中CALayer的使用

效果:

Use JTNumberScrollAnimatedView for have a nice animation for display number. It's easy to use, easy to customize.

使用 JTNumberScrollAnimatedView来展示一个效果非常不错的显示数字变化的动画效果的控件,使用很简单,非常容易定制。

Usage

Basic usage - 基本的使用

You can use JTNumberScrollAnimatedView like a normal view.

你可以像使用一个普通的view一样来使用JTNumberScrollAnimatedView

  1. #import <UIKit/UIKit.h>
  2.  
  3. #import "JTNumberScrollAnimatedView.h"
  4.  
  5. @interface ViewController : UIViewController
  6.  
  7. @property (weak, nonatomic) IBOutlet JTNumberScrollAnimatedView *animatedView;
  8.  
  9. @end

You just have to call setValue with a NSNumber and use startAnimation for launch the animation.

你只需要调用setValue方法然后执行startAnimation方法就能显示效果。

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. [self.animatedView setValue:@249];
  6. }
  7.  
  8. - (void)viewDidAppear:(BOOL)animated
  9. {
  10. [super viewDidAppear:animated];
  11.  
  12. [self.animatedView startAnimation];
  13. }

WARNING - 注意

For now the value must be a positive integer.

现在,这个值必须是正的不能是负数。

Customization - 定制

You can easily change some properties of the animation. Each caracter have its own column.

你可以很简单的修改以下的一些属性

  • textColor
  • font
  • duration
  • durationOffset, delay between the end of the animation of each column
  • density, number of characters by column for the animation
  • minLength, you can force the minimum count of columns
  • isAscending, the direction of the scroll

If you change one of this properties, you have to call setValue for update the view.

如果你修改了其中的一个属性,你需要调用 setValue 来更新画面。

Requirements

  • iOS 7 or higher                                 iOS7 或者更高版本
  • Automatic Reference Counting (ARC)  ARC
  1. //
  2. // JTNumberScrollAnimatedView.h
  3. // JTNumberScrollAnimatedView
  4. //
  5. // Created by Jonathan Tribouharet
  6. //
  7.  
  8. #import <UIKit/UIKit.h>
  9.  
  10. @interface JTNumberScrollAnimatedView : UIView
  11.  
  12. @property (strong, nonatomic) NSNumber *value;
  13.  
  14. @property (strong, nonatomic) UIColor *textColor;
  15. @property (strong, nonatomic) UIFont *font;
  16. @property (assign, nonatomic) CFTimeInterval duration;
  17. @property (assign, nonatomic) CFTimeInterval durationOffset;
  18. @property (assign, nonatomic) NSUInteger density;
  19. @property (assign, nonatomic) NSUInteger minLength;
  20. @property (assign, nonatomic) BOOL isAscending;
  21.  
  22. - (void)startAnimation;
  23. - (void)stopAnimation;
  24.  
  25. @end
  1. //
  2. // JTNumberScrollAnimatedView.m
  3. // JTNumberScrollAnimatedView
  4. //
  5. // Created by Jonathan Tribouharet
  6. //
  7.  
  8. #import "JTNumberScrollAnimatedView.h"
  9.  
  10. @interface JTNumberScrollAnimatedView(){
  11. NSMutableArray *numbersText;
  12. NSMutableArray *scrollLayers;
  13. NSMutableArray *scrollLabels;
  14. }
  15.  
  16. @end
  17.  
  18. @implementation JTNumberScrollAnimatedView
  19.  
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if(!self){
  24. return nil;
  25. }
  26.  
  27. [self commonInit];
  28.  
  29. return self;
  30. }
  31.  
  32. - (id)initWithCoder:(NSCoder *)aDecoder
  33. {
  34. self = [super initWithCoder:aDecoder];
  35. if(!self){
  36. return nil;
  37. }
  38.  
  39. [self commonInit];
  40.  
  41. return self;
  42. }
  43.  
  44. - (void)commonInit
  45. {
  46. self.duration = 1.5;
  47. self.durationOffset = .;
  48. self.density = ;
  49. self.minLength = ;
  50. self.isAscending = NO;
  51.  
  52. self.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
  53. self.textColor = [UIColor blackColor];
  54.  
  55. numbersText = [NSMutableArray new];
  56. scrollLayers = [NSMutableArray new];
  57. scrollLabels = [NSMutableArray new];
  58. }
  59.  
  60. - (void)setValue:(NSNumber *)value
  61. {
  62. self->_value = value;
  63.  
  64. [self prepareAnimations];
  65. }
  66.  
  67. - (void)startAnimation
  68. {
  69. [self prepareAnimations];
  70. [self createAnimations];
  71. }
  72.  
  73. - (void)stopAnimation
  74. {
  75. for(CALayer *layer in scrollLayers){
  76. [layer removeAnimationForKey:@"JTNumberScrollAnimatedView"];
  77. }
  78. }
  79.  
  80. - (void)prepareAnimations
  81. {
  82. for(CALayer *layer in scrollLayers){
  83. [layer removeFromSuperlayer];
  84. }
  85.  
  86. [numbersText removeAllObjects];
  87. [scrollLayers removeAllObjects];
  88. [scrollLabels removeAllObjects];
  89.  
  90. [self createNumbersText];
  91. [self createScrollLayers];
  92. }
  93.  
  94. - (void)createNumbersText
  95. {
  96. NSString *textValue = [self.value stringValue];
  97.  
  98. for(NSInteger i = ; i < (NSInteger)self.minLength - (NSInteger)[textValue length]; ++i){
  99. [numbersText addObject:@""];
  100. }
  101.  
  102. for(NSUInteger i = ; i < [textValue length]; ++i){
  103. [numbersText addObject:[textValue substringWithRange:NSMakeRange(i, )]];
  104. }
  105. }
  106.  
  107. - (void)createScrollLayers
  108. {
  109. CGFloat width = roundf(CGRectGetWidth(self.frame) / numbersText.count);
  110. CGFloat height = CGRectGetHeight(self.frame);
  111.  
  112. for(NSUInteger i = ; i < numbersText.count; ++i){
  113. CAScrollLayer *layer = [CAScrollLayer layer];
  114. layer.frame = CGRectMake(roundf(i * width), , width, height);
  115. [scrollLayers addObject:layer];
  116. [self.layer addSublayer:layer];
  117. }
  118.  
  119. for(NSUInteger i = ; i < numbersText.count; ++i){
  120. CAScrollLayer *layer = scrollLayers[i];
  121. NSString *numberText = numbersText[i];
  122. [self createContentForLayer:layer withNumberText:numberText];
  123. }
  124. }
  125.  
  126. - (void)createContentForLayer:(CAScrollLayer *)scrollLayer withNumberText:(NSString *)numberText
  127. {
  128. NSInteger number = [numberText integerValue];
  129. NSMutableArray *textForScroll = [NSMutableArray new];
  130.  
  131. for(NSUInteger i = ; i < self.density + ; ++i){
  132. [textForScroll addObject:[NSString stringWithFormat:@"%ld", (number + i) % ]];
  133. }
  134.  
  135. [textForScroll addObject:numberText];
  136.  
  137. if(!self.isAscending){
  138. textForScroll = [[[textForScroll reverseObjectEnumerator] allObjects] mutableCopy];
  139. }
  140.  
  141. CGFloat height = ;
  142. for(NSString *text in textForScroll){
  143. UILabel * textLabel = [self createLabel:text];
  144. textLabel.frame = CGRectMake(, height, CGRectGetWidth(scrollLayer.frame), CGRectGetHeight(scrollLayer.frame));
  145. [scrollLayer addSublayer:textLabel.layer];
  146. [scrollLabels addObject:textLabel];
  147. height = CGRectGetMaxY(textLabel.frame);
  148. }
  149. }
  150.  
  151. - (UILabel *)createLabel:(NSString *)text
  152. {
  153. UILabel *view = [UILabel new];
  154.  
  155. view.textColor = self.textColor;
  156. view.font = self.font;
  157. view.textAlignment = NSTextAlignmentCenter;
  158.  
  159. view.text = text;
  160.  
  161. return view;
  162. }
  163.  
  164. - (void)createAnimations
  165. {
  166. CFTimeInterval duration = self.duration - ([numbersText count] * self.durationOffset);
  167. CFTimeInterval offset = ;
  168.  
  169. for(CALayer *scrollLayer in scrollLayers){
  170. CGFloat maxY = [[scrollLayer.sublayers lastObject] frame].origin.y;
  171.  
  172. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.y"];
  173. animation.duration = duration + offset;
  174. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  175.  
  176. if(self.isAscending){
  177. animation.fromValue = [NSNumber numberWithFloat:-maxY];
  178. animation.toValue = @;
  179. }
  180. else{
  181. animation.fromValue = @;
  182. animation.toValue = [NSNumber numberWithFloat:-maxY];
  183. }
  184.  
  185. [scrollLayer addAnimation:animation forKey:@"JTNumberScrollAnimatedView"];
  186.  
  187. offset += self.durationOffset;
  188. }
  189. }
  190.  
  191. @end

[翻译] JTNumberScrollAnimatedView的更多相关文章

  1. 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...

  2. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  3. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  4. 【探索】机器指令翻译成 JavaScript

    前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...

  5. 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...

  6. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

  7. 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...

  8. 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?

    0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点

    在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...

随机推荐

  1. 如何查看yum安装路径

    #yum install hdf5 #rpm -qa|grep hdf5 hdf5-1.8.7-1.el6.rf.x86_64 #rpm -ql hdf5-1.8.7-1.el6.rf.x86_64

  2. 使用单体模式设计原生js插件

    ----------基于上次写的jquery插件进行改造  http://www.cnblogs.com/GerryOfZhong/p/5533773.html 背景:jQuery插件依赖jQuery ...

  3. C#(winform)设置窗体的启动位置

    只需要设置窗体的StartPosition属性: registerForm.StartPosition = FormStartPosition.CenterScreen; FormStartPosit ...

  4. uvm_config_db在UVM验证环境中的应用

    如何在有效的使用uvm_config_db来搭建uvm验证环境对于许多验证团队来说仍然是一个挑战.一些验证团队完全避免使用它,这样就不能够有效利用它带来的好处:另一些验证团队却过多的使用它,这让验证环 ...

  5. 基于node安装gulp-一些命令

    1.win+r:进入cdm 2.node -v:查询是否安装node 3.npm install -g gulp:安装gulp 4.gulp -v:查看是否安装gulp 5.cd desk:进入桌面 ...

  6. git常用小操作。-- 自用

    编辑 .gitignore bin-debug/  忽略所有的叫bin-debug文件夹和他下面的文件 编辑 .git/config [core] repositoryformatversion = ...

  7. 给Solr配置中文分词器

    第一步下载分词器https://pan.baidu.com/s/1X8v65YZ4gIkNQXsXfSULBw 第二歩打开已经解压的ik分词器文件夹 将ik-analyzer-solr5-5.x.ja ...

  8. C# 学习笔记(一) Winform利用Assembly反射动态创建窗体

    1. 添加Reflection //添加对Reflection程序集引用 using System.Reflection; // 引用窗体创建方法CreateForm,传入参数 private voi ...

  9. Shiro官方快速入门10min例子源码解析框架1-初始化

    Shiro,一个易用的Java安全框架,主要集合身份认证.授权.加密和session管理的功能. 这系文章主要简介Shiro架构,并通过官方的quickstart例程分析最简实现下Shiro的工作流程 ...

  10. Cookie介绍

    1.Http协议与Cookie Cookie(小量信息)是HTTP协议指定的!先由服务器保存Cookie到浏览器,而下次浏览器请求服务器时把上一次请求得到Cookie再归还给服务器 由服务器创建保存到 ...