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

VicTQUP 8年前發布 | 76K 次閱讀 IOS iOS開發 移動開發

一、下載UICountingLabel

下載地址: https://github.com/dataxpress/UICountingLabel
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];

效果圖如下:

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

整數樣式

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];

效果圖如下:

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

浮點數樣式

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

由于UICountingLabel沒有這種樣式, 所以稍微需要修改一下UICountingLabel文件.
首先在UICountingLabel.h頭文件中增加一個屬性, 如下圖:

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

添加positiveFormat屬性


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

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

添加此段代碼

這樣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];

效果圖如下:

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

帶有千分位分隔符的浮點數


 

文/jianshu_wl(簡書)
 

 

 

 本文由用戶 VicTQUP 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!