金额跳动效果
前言
金额效果,由于觉得公司目前的金额太乏味,决定加点效果,也特此写了个小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)]]; } }
上一篇 目录 已是最后