[翻译] JTNumberScrollAnimatedView
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
#import <UIKit/UIKit.h> #import "JTNumberScrollAnimatedView.h" @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet JTNumberScrollAnimatedView *animatedView; @end
You just have to call setValue with a NSNumber and use startAnimation for launch the animation.
你只需要调用setValue方法然后执行startAnimation方法就能显示效果。
- (void)viewDidLoad
{
[super viewDidLoad]; [self.animatedView setValue:@249];
} - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated]; [self.animatedView startAnimation];
}
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.
你可以很简单的修改以下的一些属性
textColorfontdurationdurationOffset, delay between the end of the animation of each columndensity, number of characters by column for the animationminLength, you can force the minimum count of columnsisAscending, 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
//
// JTNumberScrollAnimatedView.h
// JTNumberScrollAnimatedView
//
// Created by Jonathan Tribouharet
// #import <UIKit/UIKit.h> @interface JTNumberScrollAnimatedView : UIView @property (strong, nonatomic) NSNumber *value; @property (strong, nonatomic) UIColor *textColor;
@property (strong, nonatomic) UIFont *font;
@property (assign, nonatomic) CFTimeInterval duration;
@property (assign, nonatomic) CFTimeInterval durationOffset;
@property (assign, nonatomic) NSUInteger density;
@property (assign, nonatomic) NSUInteger minLength;
@property (assign, nonatomic) BOOL isAscending; - (void)startAnimation;
- (void)stopAnimation; @end
//
// JTNumberScrollAnimatedView.m
// JTNumberScrollAnimatedView
//
// Created by Jonathan Tribouharet
// #import "JTNumberScrollAnimatedView.h" @interface JTNumberScrollAnimatedView(){
NSMutableArray *numbersText;
NSMutableArray *scrollLayers;
NSMutableArray *scrollLabels;
} @end @implementation JTNumberScrollAnimatedView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(!self){
return nil;
} [self commonInit]; return self;
} - (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(!self){
return nil;
} [self commonInit]; return self;
} - (void)commonInit
{
self.duration = 1.5;
self.durationOffset = .;
self.density = ;
self.minLength = ;
self.isAscending = NO; self.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
self.textColor = [UIColor blackColor]; numbersText = [NSMutableArray new];
scrollLayers = [NSMutableArray new];
scrollLabels = [NSMutableArray new];
} - (void)setValue:(NSNumber *)value
{
self->_value = value; [self prepareAnimations];
} - (void)startAnimation
{
[self prepareAnimations];
[self createAnimations];
} - (void)stopAnimation
{
for(CALayer *layer in scrollLayers){
[layer removeAnimationForKey:@"JTNumberScrollAnimatedView"];
}
} - (void)prepareAnimations
{
for(CALayer *layer in scrollLayers){
[layer removeFromSuperlayer];
} [numbersText removeAllObjects];
[scrollLayers removeAllObjects];
[scrollLabels removeAllObjects]; [self createNumbersText];
[self createScrollLayers];
} - (void)createNumbersText
{
NSString *textValue = [self.value stringValue]; for(NSInteger i = ; i < (NSInteger)self.minLength - (NSInteger)[textValue length]; ++i){
[numbersText addObject:@""];
} for(NSUInteger i = ; i < [textValue length]; ++i){
[numbersText addObject:[textValue substringWithRange:NSMakeRange(i, )]];
}
} - (void)createScrollLayers
{
CGFloat width = roundf(CGRectGetWidth(self.frame) / numbersText.count);
CGFloat height = CGRectGetHeight(self.frame); for(NSUInteger i = ; i < numbersText.count; ++i){
CAScrollLayer *layer = [CAScrollLayer layer];
layer.frame = CGRectMake(roundf(i * width), , width, height);
[scrollLayers addObject:layer];
[self.layer addSublayer:layer];
} for(NSUInteger i = ; i < numbersText.count; ++i){
CAScrollLayer *layer = scrollLayers[i];
NSString *numberText = numbersText[i];
[self createContentForLayer:layer withNumberText:numberText];
}
} - (void)createContentForLayer:(CAScrollLayer *)scrollLayer withNumberText:(NSString *)numberText
{
NSInteger number = [numberText integerValue];
NSMutableArray *textForScroll = [NSMutableArray new]; for(NSUInteger i = ; i < self.density + ; ++i){
[textForScroll addObject:[NSString stringWithFormat:@"%ld", (number + i) % ]];
} [textForScroll addObject:numberText]; if(!self.isAscending){
textForScroll = [[[textForScroll reverseObjectEnumerator] allObjects] mutableCopy];
} CGFloat height = ;
for(NSString *text in textForScroll){
UILabel * textLabel = [self createLabel:text];
textLabel.frame = CGRectMake(, height, CGRectGetWidth(scrollLayer.frame), CGRectGetHeight(scrollLayer.frame));
[scrollLayer addSublayer:textLabel.layer];
[scrollLabels addObject:textLabel];
height = CGRectGetMaxY(textLabel.frame);
}
} - (UILabel *)createLabel:(NSString *)text
{
UILabel *view = [UILabel new]; view.textColor = self.textColor;
view.font = self.font;
view.textAlignment = NSTextAlignmentCenter; view.text = text; return view;
} - (void)createAnimations
{
CFTimeInterval duration = self.duration - ([numbersText count] * self.durationOffset);
CFTimeInterval offset = ; for(CALayer *scrollLayer in scrollLayers){
CGFloat maxY = [[scrollLayer.sublayers lastObject] frame].origin.y; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.y"];
animation.duration = duration + offset;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; if(self.isAscending){
animation.fromValue = [NSNumber numberWithFloat:-maxY];
animation.toValue = @;
}
else{
animation.fromValue = @;
animation.toValue = [NSNumber numberWithFloat:-maxY];
} [scrollLayer addAnimation:animation forKey:@"JTNumberScrollAnimatedView"]; offset += self.durationOffset;
}
} @end
[翻译] JTNumberScrollAnimatedView的更多相关文章
- 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- 【探索】机器指令翻译成 JavaScript
前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
- 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...
- 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?
0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
随机推荐
- js读取cookie信息
1. 第一种方式读取cookie信息:用document.cookie.split(“; “)的方式把字符串分割成几个段,然后遍历整个数组 //javascript方法 function getCoo ...
- 微服务Kong(五)——添加一个用户(Consumer)
在本节中,我们将学习如何添加一个用户(consumer)到KONG实例中.用户是与使用您的API的个人相关联,可用于跟踪,访问管理等. NOTE:本节假设您已经正确启用了密钥验证插件.如果没有,请参考 ...
- MYSQL数据库的参数文件
参数文件:告诉MySQL实例启动时在哪里可以找到数据库文件,并且指定某些初始化参数,这些参数定义了某种内存结构的大小等设置,还会介绍各种参数的类型. 参数文件 当MySQL实例启动时,MySQL会先去 ...
- vue数组进行分组
数组进行分组使用switch方法 <template> <v-layout> <v-card contextual-style="dark" v-if ...
- linux centos 7.5 安装mysql5.7
1 下载tar包,这里使用wget从官网下载(注:下载地址随时可能有变动,wget命令默认下载目录为当前所在文件夹) wget https://dev.mysql.com/get/Downloads/ ...
- R语言数组array函数
数组是一个可以在两个以上的维度存储数据的R数据对象.例如 - 如果创建尺寸(2,3,4)的数组,那么创建4个矩形矩阵每2行3列.数组只能存储数据类型. 使用 array()函数创建数组.它需要向量作为 ...
- 为什么各大网站都纷纷用起了https?哪些网站需要https(SSL证书)
其实最近我也在易维信网站的开发组内讨论应用全站https事宜. 其原因非常简单. 因为不断接到用户投诉说网站上出现影响浏览体验的大面积广告. 可是网站平常只针对未登录用户在顶栏和底栏打两小条广告.而且 ...
- Java工具:native2ascii ---得到中文对应的ASCII编码
如国际化中,要得到“提交”对应的ASCII编码:
- C#实现文件下载的几种方式
上篇博客也说了下C#中通过XML导出Excel,这些文件操作都挺有用的,下面是文件的下载,之前做项目都是把它写的空间日志中,以后有时间了把它们都弄出来 先把有问题的代码贴出来吧 using Syste ...
- WeifenLuo.WinFormsUI.Docking.dll的用法
基本框架 说明 weiFenLuo.winFormsUI.Docking.dll是开源项目DockPanel Suite的一个类库,可以实现像Visual Studio的窗口停靠.拖拽等功能.Weif ...