Подтвердить что ты не робот

Анимация текста Содержание в iOS - эквивалент для Android ValueAnimator

Я работаю над приложением iOS 7+ и хотел бы анимировать изменение содержимого UILabel. Я не хочу делать какую-либо графическую анимацию, например, исчезать из старого контента/исчезать в новом контенте. Таким образом, все стандартные функции анимации iOS, такие как анимация Layer или блоки анимации, не могут использоваться (по крайней мере, я так думаю).

Предположим, что UILabel показывает некоторые значения в метрах, например "200 В" , и этот текст следует изменить на "400 В". Текст должен не просто переходить с "200 В" на "400 В", а подсчитываться с использованием некоторой функции ослабления: "200 В" , "220 В", "240 В"... "390 В", "395 V"... "400 В"

В Android можно легко решить с помощью ValueAnimator:

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setInterpolation(new EaseOutInterpolator());
animation.setDuration(2500);
animation.setStartDelay(500);

animation.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpate(ValueAnimator animator) {
        float currentValue = animator.getAnimatedValue.floatValue();
        label1.setText(String.format("%.2", fromValue1 + ((toValue1 - fromValue1) * currentValue)));
        label2.setText(String.format("%.2", fromValue2 + ((toValue2 - fromValue2) * currentValue)));
    ...
    }
});
animation.start();

Есть ли такая вещь в iOS? Я нашел другое решение для этого, но все они довольно старые (2010/11), и все это в конечном итоге реализует это поведение вручную, используя NSTimer и собственные функции ослабления.

Не исключено, что это можно реализовать самостоятельно, но это было бы довольно громоздким и не очень элегантным. Итак: есть ли что-то в iOS для решения этой проблемы или существует ли хотя бы удобная сторонняя реализация?

Большое спасибо!

4b9b3361

Ответ 1

Поскольку я не нашел подходящего решения для этого, я создал свой собственный: простой класс Animator, который обрабатывает Easing:

// MyValueAnimation.h
typedef void (^MyAnimationBlock)(double animationValue);

@interface MyValueAnimation : NSObject

- (void)startAnimation:(MyAnimationBlock)animationBlock runtime:(NSUInteger)runtime delay:(NSUInteger)delay;

@end


// MyValueAnimation.m
#import "MyValueAnimation.h"

// Number of seconds between each animation step
#define kStepSize 0.05

@interface MyValueAnimation () {
    NSTimer *timer;            
    NSUInteger totalRunTime;    // Total duration of the animation (delay not included)
    NSUInteger currentRuntime;  // Time the animation is already running
    MyAnimationBlock animationBlock;
}
@end

@implementation MyValueAnimation

- (void)startAnimation:(MyAnimationBlock)block runtime:(NSUInteger)runtime delay:(NSUInteger)delay {
    if (timer != nil)
        [timer invalidate];

    timer = nil;
    totalRunTime = runtime;
    animationBlock = block;
    currentRuntime = 0;

    if (block != nil) {
        if (delay > 0) {
            // Wait to delay the start. Convert delay from millis to seconds
            double delaySeconds = (double)delay / 1000.0;
            timer = [NSTimer scheduledTimerWithTimeInterval:delaySeconds target:self selector:@selector(delayTick:) userInfo:nil repeats:false];
        } else {
            // Run the animation
            timer = [NSTimer scheduledTimerWithTimeInterval:kStepSize target:self selector:@selector(animationTick:) userInfo:nil repeats:true];
        }
    }
}

- (void)delayTick:(NSTimer *)delayTimer {
    // End of delay -> run animation
    [delayTimer invalidate];
    timer = [NSTimer scheduledTimerWithTimeInterval:kStepSize target:self selector:@selector(animationTick:) userInfo:nil repeats:true];
}

- (void)animationTick:(NSTimer *)animationTimer {
    NSUInteger step = 1000 * kStepSize;  // step size/length in milli seconds
    currentRuntime += step;
    double progress = MIN((double)currentRuntime / (double)totalRunTime, 1.0);

    // Progress is a value between 0 and 1. The easing function maps this
    // to the animationValue which is than used inside the animationBlock
    // to calculate the current value of the animiation
    double animationValue = [self customEaseOut:progress];

    if (animationBlock != nil)
        animationBlock(animationValue);

    if (progress >= 1.0) {
        // Animation complete
        [timer invalidate];
        timer = nil;
    }        
}

- (double)customEaseOut:(double)t {
    // Use any easing function you like to animate your values...
    // http://rechneronline.de/function-graphs/
    // http://sol.gfxile.net/interpolation/
    return (1 - pow(1-t, 2));
}

@end 


// =============================================================

// Some code using the animation
- (void)animateValueFrom:(double)fromValue to:(double)toValue {
    if (valueAnimation == nil)
        valueAnimation = [[MyValueAnimation alloc] init];

    MyAnimationBlock animationBlock = ^(double animationValue) {
        double currentValue = fromValue + ((toValue - fromValue) * animationValue);

        someLabel.text = [NSString stringWithFormat:"%dV", currentValue];        
    };

    [valueAnimation startAnimation:animationBlock runtime:1500 delay:500];
}

Возможно, это не самое приятное решение, но оно работает: -)

Ответ 2

Я знаю решение, но его для быстрой итерации, потому что окончательные итерации могут перепрыгивать через значение (выглядит не красиво, если есть медленный поиск), но решение простое и короткое, не может быть самым красивым из архитектуры (но это может исправляться, если это необходимо).

- (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];

        });

    }
}