美文网首页iOS 控件定制
iOS开发中使用UICountingLabel控件实现数字变化效

iOS开发中使用UICountingLabel控件实现数字变化效

作者: 梁森的简书 | 来源:发表于2018-06-26 10:13 被阅读112次

    效果图:

    1.gif

    代码:

      // 从1跑到10
    UICountingLabel* myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 10, 200, 40)];
    myLabel.method = UILabelCountingMethodLinear;
    myLabel.format = @"%d";
    [self.view addSubview:myLabel];
    [myLabel countFrom:1 to:10 withDuration:3.0];
    
    // make one that counts up from 5% to 10%, using ease in out (the default)
    UICountingLabel* countPercentageLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 50, 200, 40)];
    [self.view addSubview:countPercentageLabel];
    countPercentageLabel.format = @"%.1f%%";
    [countPercentageLabel countFrom:5 to:10];
    
    // 带有逗号
    // count up using a string that uses a number formatter
    UICountingLabel* scoreLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 90, 200, 40)];
    [self.view addSubview:scoreLabel];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = kCFNumberFormatterDecimalStyle;
    scoreLabel.formatBlock = ^NSString* (CGFloat value)
    {
        NSString* formatted = [formatter stringFromNumber:@((int)value)];
        return [NSString stringWithFormat:@"Score: %@",formatted];
    };
    scoreLabel.method = UILabelCountingMethodEaseOut;
    [scoreLabel countFrom:0 to:10000000 withDuration:2.5];
    

    git地址:**https://github.com/dataxpress/UICountingLabel

    相关文章

      网友评论

      • 君赏:看到 UI 开头以为我开发了这么么多年 系统什么时候出来的这个控件
        梁森的简书:不好意思,让标题误导了你😄

      本文标题:iOS开发中使用UICountingLabel控件实现数字变化效

      本文链接:https://www.haomeiwen.com/subject/uvjpyftx.html