常用的第三方橙红科技有限公司收录iOS 10优秀文章

iOS与macOS中一款优雅的数字/金额增减动效控件(支付宝内金

2016-10-20  本文已影响1701人  jkpang
PPCounter.gif

PPCounter

前言

在新的项目中UI妹子设计出了一个金额不断增加的动画,如下图:

动效图.gif

然后就找度娘学习下了相关经验,受到这篇博客的启发:ios核心动画高级技巧,使用CADisplayLink定时器来做此动效的引擎(其实使用NSTimer和GCD定时器也可以做到,但使用CADisplayLink最佳)。

现在我已经将此效果从项目中分拆出来,独立封装好了,调用一句代码就可以实现数字加减的动效

代码部分

1.1 设置一般字体属性UILabel

....
[label pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut formatBlock:^NSString *(CGFloat number) {
    // 此处自由拼接内容
    return [NSString stringWithFormat:@"%.2f",number];
} completeBlock:^{
    // 完成的回调
}];

1.2 设置富文本字体属性UILabel

....
[label pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut attributedFormatBlock:^NSAttributedString *(CGFloat number) {
        
    // 此处自由设置富文本属性的内容
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
    return attributedString;
} completeBlock:^{
    // 完成的回调
}];

2. UIButton

2.1 设置一般字体属性UIButton

....
[button pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut formatBlock:^NSString *(CGFloat number) {
    // 此处自由拼接内容
    return [NSString stringWithFormat:@"%.2f",number];
} completeBlock:^{
    // 完成的回调
}];

2.2 设置富文本字体属性UIButton

....
[button pp_fromNumber:0 toNumber:100 duration:1.5 animationType:PPCounterAnimationTypeEaseOut attributedFormatBlock:^NSAttributedString *(CGFloat number) {
        
    // 此处自由设置富文本属性的内容
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
    return attributedString;
} completeBlock:^{
        
    // 完成的回调
}];

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:^{
            // 计数完成的回调
        self.numberLabel.textColor = [NSColor redColor];
    }];

以上就是PPCounter的简单使用方法,更详细的用法请看Demo :
https://github.com/jkpang/PPCounter, 欢迎Star,欢迎Fork!

(注: 我的使用了PPCounter组件的Mac APP开源项目: PPRows --在Mac上优雅的计算你写了多少行代码)
上一篇 下一篇

猜你喜欢

热点阅读