原创:知识点总结性文章
创作不易,请珍惜,之后会持续更新,不断完善
个人比较喜欢做笔记和写总结,毕竟好记性不如烂笔头哈哈,这些文章记录了我的IOS成长历程,希望能与大家一起进步
温馨提示:由于简书不支持目录跳转,大家可通过command + F 输入目录标题后迅速寻找到你所需要的内容
目录
- 一、UICountingLabel
- Demo
- 参考文献
一、UICountingLabel
a、运行效果
UICountingLabelb、做一个算数的
UICountingLabel *myLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 100, 200, 40)];
myLabel.method = UILabelCountingMethodLinear;// 线性变化
myLabel.format = @"%d";
[self.view addSubview:myLabel];
[myLabel countFrom:1 to:10 withDuration:3.0];
c、使用ease-in-out(默认设置)将计数从5%增加到10%
UICountingLabel *countPercentageLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 170, 200, 40)];
[self.view addSubview:countPercentageLabel];
countPercentageLabel.format = @"%.1f%%";
[countPercentageLabel countFrom:5 to:10];
d、使用使用数字格式化的字符串进行计数
UICountingLabel *scoreLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 210, 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:10000 withDuration:2.5];// 可以指定动画的时长,默认时长是2.0秒
e、使用属性字符串计数
NSInteger toValue = 100;
UICountingLabel *attributedLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 270, 200, 40)];
[self.view addSubview:attributedLabel];
// 用于设置属性字符串的格式,如果指定了formatBlock,则将会覆盖掉format属性,因为block的优先级更高
attributedLabel.attributedFormatBlock = ^NSAttributedString* (CGFloat value)
{
NSDictionary *normal = @{ NSFontAttributeName: [UIFont fontWithName: @"HelveticaNeue-UltraLight" size: 20] };
NSDictionary *highlight = @{ NSFontAttributeName: [UIFont fontWithName: @"HelveticaNeue" size: 20] };
NSString *prefix = [NSString stringWithFormat:@"%d", (int)value];
NSString *postfix = [NSString stringWithFormat:@"/%d", (int)toValue];
NSMutableAttributedString *prefixAttr = [[NSMutableAttributedString alloc] initWithString: prefix attributes: highlight];
NSAttributedString *postfixAttr = [[NSAttributedString alloc] initWithString: postfix attributes: normal];
[prefixAttr appendAttributedString: postfixAttr];
return prefixAttr;
};
[attributedLabel countFrom:0 to:toValue withDuration:2.5];
f、获得动画结束的事件
self.completionBlockLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(10, 370, 200, 40)];
[self.view addSubview:self.completionBlockLabel];
self.completionBlockLabel.method = UILabelCountingMethodEaseInOut;
self.completionBlockLabel.format = @"%d%%";
__weak UICountingLabelViewController *weakSelf = self;
self.completionBlockLabel.completionBlock = ^{
weakSelf.completionBlockLabel.textColor = [UIColor colorWithRed:0 green:0.5 blue:0 alpha:1];
};
[self.completionBlockLabel countFrom:0 to:100];
Demo
Demo在我的Github上,欢迎下载。
UseFrameworkDemo
网友评论