国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS使用UICountingLabel實現數字變化的動畫效果

2020-07-26 03:00:11
字體:
來源:轉載
供稿:網友

在大多數金融類 app 上或者其他 app 需要數字展示的地方, 經常會有如下的動畫效果:

動畫效果

怎么做呢?

一、下載UICountingLabel

下載地址: http://xiazai.VeVB.COm/201612/yuanma/UICountingLabel-master_jb51.rar

UICountingLabel只支持整形和浮點數樣式, 像大部分金融類app里面顯示的金額(帶有千分位分隔符)的樣式是無法顯示的, 但是后面會給出解決方案, 實現這些的效果!

二、使用UICountingLabel

1. 初始化

UICountingLabel 繼承 UILabel, 初始化和 UILabel 一樣, 如下:

UICountingLabel* myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];[self.view addSubview:myLabel];

2. 設置文本樣式

可以這樣設置:

myLabel.format = @"%d";

也可以使用 block設置:

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. 設置變化范圍及動畫時間

[myLabel countFrom:50 to:100 withDuration:5.0f];

就這么簡單!

三、實例效果

1. 整數樣式數字的變化

代碼如下:

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];//設置格式myLabel.format = @"%d";//設置變化范圍及動畫時間[self.myLabel countFrom:0    to:100  withDuration:1.0f];

效果圖如下:

整數樣式

2. 浮點數樣式數字的變化

代碼如下:

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];//設置格式myLabel.format = @"%.2f";//設置變化范圍及動畫時間[self.myLabel countFrom:0.00    to:3198.23  withDuration:1.0f];

效果圖如下:

浮點數樣式

3. 帶有千分位分隔符的浮點數樣式

由于UICountingLabel沒有這種樣式, 所以稍微需要修改一下UICountingLabel文件.

首先在UICountingLabel.h頭文件中增加一個屬性, 如下圖:

添加positiveFormat屬性

接著在UICountingLabel.m文件里面- (void)setTextValue:(CGFloat)value方法中添加如下代碼:

添加此段代碼

這樣UICountingLabel就可以實現這種樣式了.

下面開始實現這種樣式, 代碼如下:

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];//設置格式myLabel.format = @"%.2f";//設置分隔符樣式myLabel.positiveFormat = @"###,##0.00";//設置變化范圍及動畫時間[self.myLabel countFrom:0.00   to:3048.64  withDuration:1.0f];

效果圖如下:

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 哈尔滨市| 巴东县| 井研县| 清流县| 塘沽区| 平原县| 潜江市| 舞阳县| 河源市| 石棉县| 石渠县| 嫩江县| 崇义县| 尚义县| 武义县| 光泽县| 电白县| 镶黄旗| 肥乡县| 同德县| 岳池县| 盘锦市| 高密市| 阳原县| 洞头县| 高唐县| 永仁县| 白山市| 贞丰县| 潍坊市| 海城市| 团风县| 东乡族自治县| 庐江县| 永登县| 嫩江县| 定日县| 龙陵县| 巢湖市| 萍乡市| 黄大仙区|