PPCounter.gif
PPCounter
前言
在新的項(xiàng)目中UI妹子設(shè)計(jì)出了一個(gè)金額不斷增加的動(dòng)畫(huà),如下圖:
動(dòng)效圖.gif
然后就找度娘學(xué)習(xí)下了相關(guān)經(jīng)驗(yàn),受到這篇博客的啟發(fā):ios核心動(dòng)畫(huà)高級(jí)技巧,使用CADisplayLink定時(shí)器來(lái)做此動(dòng)效的引擎(其實(shí)使用NSTimer和GCD定時(shí)器也可以做到,但使用CADisplayLink最佳)兄春。
現(xiàn)在我已經(jīng)將此效果從項(xiàng)目中分拆出來(lái),獨(dú)立封裝好了,調(diào)用一句代碼就可以實(shí)現(xiàn)數(shù)字加減的動(dòng)效
- 支持iOS/macOS雙平臺(tái)(pods版本v0.5.0, 2017.03.07更新)
- 支持UILable/UIButton/自定義文本控件的數(shù)字加減動(dòng)畫(huà);
- 支持一般文本屬性以及富文本屬性的字體顯示;
- 支持四種時(shí)間曲線函數(shù)動(dòng)畫(huà):由慢到快再到慢离例、由慢到特別快贯底、由快到慢乐纸、勻速;
- 支持自定義的文本格式,例如:數(shù)字格式化千分位顯示;
- 支持CocoaPods導(dǎo)入
代碼部分
1.1 設(shè)置一般字體屬性UILabel
....
[label pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut formatBlock:^NSString *(CGFloat number) {
// 此處自由拼接內(nèi)容
return [NSString stringWithFormat:@"%.2f",number];
} completeBlock:^{
// 完成的回調(diào)
}];
1.2 設(shè)置富文本字體屬性UILabel
....
[label pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut attributedFormatBlock:^NSAttributedString *(CGFloat number) {
// 此處自由設(shè)置富文本屬性的內(nèi)容
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} completeBlock:^{
// 完成的回調(diào)
}];
2. UIButton
2.1 設(shè)置一般字體屬性UIButton
....
[button pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut formatBlock:^NSString *(CGFloat number) {
// 此處自由拼接內(nèi)容
return [NSString stringWithFormat:@"%.2f",number];
} completeBlock:^{
// 完成的回調(diào)
}];
2.2 設(shè)置富文本字體屬性UIButton
....
[button pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut attributedFormatBlock:^NSAttributedString *(CGFloat number) {
// 此處自由設(shè)置富文本屬性的內(nèi)容
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} completeBlock:^{
// 完成的回調(diào)
}];
3, macOS Platform 使用
[[PPCounterEngine counterEngine] fromNumber:0
toNumber:999
duration:2.f
animationOptions:PPCounterAnimationOptionCurveEaseOut
currentNumber:^(CGFloat number) {
// lable控件
self.numberLabel.stringValue = [NSString stringWithFormat:@"%ld",(NSInteger)number];
} completion:^{
// 計(jì)數(shù)完成的回調(diào)
self.numberLabel.textColor = [NSColor redColor];
}];
以上就是PPCounter的簡(jiǎn)單使用方法,更詳細(xì)的用法請(qǐng)看Demo :
https://github.com/jkpang/PPCounter, 歡迎Star,歡迎Fork!