Animate text Content in iOS - Equivalent for Android ValueAnimator

后端 未结 2 1541
梦谈多话
梦谈多话 2021-01-04 07:02

I am working on an iOS 7+ app and would like to animated change the content of an UILabel. I do not want do do any graphical animation like fade out old con

2条回答
  •  北海茫月
    2021-01-04 07:18

    I know a solution but its for fast iteration because final iterations can jump through value (looks not beautifully if there is a slow search), but the decision simple and short, can not the most beautiful from a architecture(but it can be corrected if it is necessary) realisation.

    - (void)someAction 
    {
        [self animateValue:0 toValue:1111 withStep:7 andIntervalSpeed:5];
    }
    
    -(void)animateValue:(int)value toValue:(int)toValue withStep:(int)step andIntervalSpeed:(int64_t)intervalSpeed
    {
        self.currentValue = value; // @property (nonatomic) int currentValue;
        NSUInteger numberofIteration = (toValue - value)/step;
    
        int64_t interval = 0.0;
        for (NSUInteger i = 0; i < numberofIteration; i++) {
    
            dispatch_time_t start = DISPATCH_TIME_NOW;
            interval += intervalSpeed;
    
            dispatch_after(dispatch_time(start, interval * USEC_PER_SEC), dispatch_get_main_queue(), ^{
    
                if (((toValue - value)%step != 0) && (i == (numberofIteration-1)))
                {
                    self.currentValue = toValue;
                }
                else
                {
                    self.currentValue+= step;
                }
    
                NSLog(@"%d",self.currentValue);
                self.someLabel.text = [NSString stringWithFormat:@"%d",self.currentValue];
    
            });
    
        }
    }
    

提交回复
热议问题