iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化的動(dòng)畫效果
在大多數(shù)金融類 app 上或者其他 app 需要數(shù)字展示的地方, 經(jīng)常會(huì)有如下的動(dòng)畫效果:
動(dòng)畫效果
怎么做呢?
一、下載UICountingLabel
下載地址: http://xiazai.jb51.net/201612/yuanma/UICountingLabel-master_jb51.rar
UICountingLabel只支持整形和浮點(diǎn)數(shù)樣式, 像大部分金融類app里面顯示的金額(帶有千分位分隔符)的樣式是無法顯示的, 但是后面會(huì)給出解決方案, 實(shí)現(xiàn)這些的效果!
二、使用UICountingLabel
1. 初始化
UICountingLabel 繼承 UILabel, 初始化和 UILabel 一樣, 如下:
UICountingLabel* myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)]; [self.view addSubview:myLabel];
2. 設(shè)置文本樣式
可以這樣設(shè)置:
myLabel.format = @"%d";
也可以使用 block設(shè)置:
myLabel.formatBlock = ^NSString* (CGFloat value) { NSInteger years = value / 12; NSInteger months = (NSInteger)value % 12; if (years == 0) { return [NSString stringWithFormat: @"%ld months", (long)months]; } else { return [NSString stringWithFormat: @"%ld years, %ld months", (long)years, (long)months]; } };
3. 設(shè)置變化范圍及動(dòng)畫時(shí)間
[myLabel countFrom:50 to:100 withDuration:5.0f];
就這么簡(jiǎn)單!
三、實(shí)例效果
1. 整數(shù)樣式數(shù)字的變化
代碼如下:
UICountingLabel *myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(titleLabel.frame)+1, 280, 45)]; myLabel.textAlignment = NSTextAlignmentCenter; myLabel.font = [UIFont fontWithName:@"Avenir Next" size:48]; myLabel.textColor = [UIColor colorWithRed:236/255.0 green:66/255.0 blue:43/255.0 alpha:1]; [self.view addSubview:myLabel]; //設(shè)置格式 myLabel.format = @"%d"; //設(shè)置變化范圍及動(dòng)畫時(shí)間 [self.myLabel countFrom:0 to:100 withDuration:1.0f];
效果圖如下:
整數(shù)樣式
2. 浮點(diǎn)數(shù)樣式數(shù)字的變化
代碼如下:
UICountingLabel *myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(titleLabel.frame)+1, 280, 45)]; myLabel.textAlignment = NSTextAlignmentCenter; myLabel.font = [UIFont fontWithName:@"Avenir Next" size:48]; myLabel.textColor = [UIColor colorWithRed:236/255.0 green:66/255.0 blue:43/255.0 alpha:1]; [self.view addSubview:myLabel]; //設(shè)置格式 myLabel.format = @"%.2f"; //設(shè)置變化范圍及動(dòng)畫時(shí)間 [self.myLabel countFrom:0.00 to:3198.23 withDuration:1.0f];
效果圖如下:
浮點(diǎn)數(shù)樣式
3. 帶有千分位分隔符的浮點(diǎn)數(shù)樣式
由于UICountingLabel沒有這種樣式, 所以稍微需要修改一下UICountingLabel文件.
首先在UICountingLabel.h頭文件中增加一個(gè)屬性, 如下圖:
添加positiveFormat屬性
接著在UICountingLabel.m文件里面- (void)setTextValue:(CGFloat)value方法中添加如下代碼:
添加此段代碼
這樣UICountingLabel就可以實(shí)現(xiàn)這種樣式了.
下面開始實(shí)現(xiàn)這種樣式, 代碼如下:
UICountingLabel *myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(titleLabel.frame)+1, 280, 45)]; myLabel.textAlignment = NSTextAlignmentCenter; myLabel.font = [UIFont fontWithName:@"Avenir Next" size:48]; myLabel.textColor = [UIColor colorWithRed:236/255.0 green:66/255.0 blue:43/255.0 alpha:1]; [self.view addSubview:myLabel]; //設(shè)置格式 myLabel.format = @"%.2f"; //設(shè)置分隔符樣式 myLabel.positiveFormat = @"###,##0.00"; //設(shè)置變化范圍及動(dòng)畫時(shí)間 [self.myLabel countFrom:0.00 to:3048.64 withDuration:1.0f];
效果圖如下:
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
iOS開發(fā) widget構(gòu)建詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS開發(fā) widget構(gòu)建詳解的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-11-11詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06iOS開發(fā)中class和#import的區(qū)別介紹
這篇文章主要介紹了iOS開發(fā)中class和#import的區(qū)別,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02iOS實(shí)現(xiàn)聯(lián)系人按照首字母進(jìn)行排序的實(shí)例
下面小編就為大家分享一篇iOS實(shí)現(xiàn)聯(lián)系人按照首字母進(jìn)行排序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12OC - 9.基于Quartz2D繪制下載進(jìn)度條(demo)
這篇文章主要介紹了OC - 9.基于Quartz2D繪制下載進(jìn)度條(demo)的相關(guān)資料,需要的朋友可以參考下2015-11-11