在新的項目中UI妹子設計出了一個相似於支付寶金額不斷增長的動畫,以下圖: html
而後就找度娘學習下了相關經驗,受到這篇博客的啓發:ios核心動畫高級技巧,使用CADisplayLink定時器來作此動效的引擎(其實使用NSTimer和GCD定時器也能夠作到,但使用CADisplayLink最佳)。ios
....
[label pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut formatBlock:^NSString *(CGFloat number) {
// 此處自由拼接內容
return [NSString stringWithFormat:@"%.2f",number];
} completeBlock:^{
// 完成的回調
}];複製代碼
....
[label pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut attributedFormatBlock:^NSAttributedString *(CGFloat number) {
// 此處自由設置富文本屬性的內容
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} completeBlock:^{
// 完成的回調
}];複製代碼
....
[button pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut formatBlock:^NSString *(CGFloat number) {
// 此處自由拼接內容
return [NSString stringWithFormat:@"%.2f",number];
} completeBlock:^{
// 完成的回調
}];複製代碼
....
[button pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut attributedFormatBlock:^NSAttributedString *(CGFloat number) {
// 此處自由設置富文本屬性的內容
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
return attributedString;
} completeBlock:^{
// 完成的回調
}];複製代碼
[[PPCounterEngine counterEngine] fromNumber:0
toNumber:999
duration:2.f
animationOptions:PPCounterAnimationOptionCurveEaseOut
currentNumber:^(CGFloat number) {
// lable控件
self.numberLabel.stringValue = [NSString stringWithFormat:@"%ld",(NSInteger)number];
} completion:^{
// 計數完成的回調
self.numberLabel.textColor = [NSColor redColor];
}];複製代碼
以上就是PPCounter的簡單使用方法,更詳細的用法請看Demo : github.com/jkpang/PPCo…, 歡迎Star,歡迎Fork!git