继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

金额跳动动画效果

Dwyane_Coding
关注TA
已关注
手记 36
粉丝 6445
获赞 781

金额跳动效果

前言

金额效果,因为觉得公司目前的金额太乏味,决定加点效果,也特此写了个小demo,代码非常简单,贴代码方便大家看看

通过 runtime 建立属性(setter/getter方法)

/** 由于分类中要添加属性,所以通过runtime方法来实现 */
- (void)setAnimatedNumber:(NSNumber *)animatedNumber {
    objc_setAssociatedObject(self, "animatedNumber", animatedNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)animatedNumber {
    return objc_getAssociatedObject(self, "animatedNumber");
}

- (void)setTimer:(NSTimer *)timer {
    objc_setAssociatedObject(self, "timer", timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimer *)timer {
    return objc_getAssociatedObject(self, "timer");
}

- (void)setValueString:(NSString *)valueString {
    objc_setAssociatedObject(self, "valueString", valueString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)valueString {
    return objc_getAssociatedObject(self, "valueString");
}

变量初始化以及定时器

- (void)dw_setNumber:(NSNumber *)number {
    [self dw_setNumber:number duration:5.0];  //这里,我写5秒,是为了方便我录gif,使用中可以改为1或者2
}

显示金额动画

- (void)changeAnimation:(NSTimer *)timer{

    NSMutableDictionary *info = timer.userInfo;
    int begin = (int)[info objectForKey:kBeginNumberKey];
    int end = ((NSNumber *)[info objectForKey:kEndNumberKey]).intValue;
    int range = ((NSNumber *)[info objectForKey:kRangeNumberKey]).intValue;

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterNoStyle];
    [formatter setFormatWidth:9];
    [formatter setPositiveFormat:@",##0.00"];

    int value = self.valueString.intValue;

    if (value == 0) {
        self.valueString = [NSString stringWithFormat:@"%d", begin];
        self.text = [NSString stringWithFormat:@"%.@",[formatter stringFromNumber:@(begin)]];
    } else if (value >= end) {
        self.text = [NSString stringWithFormat:@"%.@",[formatter stringFromNumber:@(end)]];
        [self.timer invalidate];
        self.timer = nil;
        return;
    } else {
        value += range;
        self.valueString = [NSString stringWithFormat:@"%d", value];
        self.text = [NSString stringWithFormat:@"%.@",[formatter stringFromNumber:@(value)]];

    }

}



代码传送门

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP