GithubHelp home page GithubHelp logo

msnumberscrollanimatedview's People

Contributors

mrsongzj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

msnumberscrollanimatedview's Issues

加了个小数点支持,需要的可以拷贝

NSMutableArray *_replaceArr; //保存替换占位的数字,赋值与释放需要自己操作一下

//字体剧中以及超出宽度省略

  • (void)configNumbersText
    {
    NSString *numberStr = [_number stringValue];
    // 如果 number 长度小于 最小长度就补0
    // 这里需要注意一下 minLength 和 length 都是NSUInteger类型 如果相减得负数的话会有问题
    for (NSInteger i = 0; i < (NSInteger)self.minLength - (NSInteger)numberStr.length; i++) {
    [_numbersText addObject:@"0"];
    [_replaceArr addObject:@"0"];
    }
    // 取出 number 各位数
    for (NSUInteger i = 0; i < numberStr.length; i++) {
    [_numbersText addObject:[numberStr substringWithRange:NSMakeRange(i, 1)]];

      NSString *subNumber = [numberStr substringWithRange:NSMakeRange(i, 1)];
      [_replaceArr addObject: [subNumber isEqualToString:@"1"] ? @"0" : subNumber];
    

    }
    }

  • (void)configScrollLayers
    {
    //替换1为0,补充宽度占位
    NSString *replaceStr = [_number.stringValue stringByReplacingOccurrencesOfString:@"1" withString:@"0"];

    //得到文字总长度来计算文字剧中
    CGRect tempRect = [replaceStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width,CGRectGetHeight(self.frame))options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil];

    // 平均分配宽度
    CGFloat height = CGRectGetHeight(self.frame);
    CGFloat starX = (CGRectGetWidth(self.frame) - tempRect.size.width) / 2;
    starX = starX < 0 ? 0 : starX;

    // 创建和配置 scrollLayer
    for (NSUInteger i = 0; i < _numbersText.count; i++) {
    NSString *replaceNumberText = _replaceArr[i];

      //单个文字宽度
      CGFloat width = [replaceNumberText boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame),CGRectGetHeight(self.frame))options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil].size.width;
    
      CAScrollLayer *layer = [CAScrollLayer layer];
      layer.frame = CGRectMake(starX, 0, width, height);
      [_scrollLayers addObject:layer];
      [self.layer addSublayer:layer];
      
      starX += width;
    
      //文字长度大于控件长度显示省略号,10是默认..省略号的长度
      NSString *numberText = (CGRectGetWidth(self.frame) < tempRect.size.width && (CGRectGetWidth(self.frame) - starX) < 10) ? @".." : _numbersText[i];
      [self configScrollLayer:layer numberText:numberText];
      
      if ([numberText isEqualToString:@".."]) {return;}
    

    }
    }

//小数点逻辑

  • (void)configScrollLayer:(CAScrollLayer *)layer numberText:(NSString *)numberText
    {
    NSMutableArray *scrollNumbers = [NSMutableArray array];
    if ([numberText isEqualToString:@"."] || [numberText isEqualToString:@".."]) {
    [scrollNumbers addObject:numberText];
    }else {
    NSInteger number = [numberText floatValue];
    // 添加要滚动的数字
    for (NSInteger i = 0; i < self.density + 1; i++) {
    [scrollNumbers addObject:[NSString stringWithFormat:@"%u", (unsigned int)((number+i) % 10)]];
    }
    [scrollNumbers addObject:numberText];
    }

    // 创建 scrollLayer 的内容,数字降序排序
    // 修改局部变量的值需要使用 __block 修饰符
    __block CGFloat height = 0;
    [scrollNumbers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSString *text, NSUInteger idx, BOOL * _Nonnull stop) {
    UILabel *label = [self createLabel:text];
    label.frame = CGRectMake(0, height, CGRectGetWidth(layer.frame), CGRectGetHeight(layer.frame));
    [layer addSublayer:label.layer];
    // 保存label,防止对象被回收
    [_scrollLabels addObject:label];
    // 累加高度
    height = CGRectGetMaxY(label.frame);
    }];
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.