preface

Amount effect, because the company’s current amount is too boring, decided to add effect, also hereby write a small demo, the code is very simple, paste code for everyone to have a look

Setting up properties via Runtime (setter/getter methods)

*/ - (void); */ - (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");
}
Copy the code

Variable initialization and timer

- (void)dw_setNumber:(NSNumber *)number {[self dw_setNumber:number duration:5.0]; // Here, I write 5 seconds, is convenient for me to record GIF, can be changed to 1 or 2}Copy the code

Display amount animation

- (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)]]; }}Copy the code



Code portal