gpt4 book ai didi

ios - iOS 中的动画文本内容 - 相当于 Android ValueAnimator

转载 作者:技术小花猫 更新时间:2023-10-29 10:27:24 25 4
gpt4 key购买 nike

我正在开发一个 iOS 7+ 应用程序,并且想要动画更改 UILabel 的内容。我不想做任何图形动画,如淡出旧内容/淡入新内容。因此,无法使用 iOS 提供的所有标准动画功能,如图层动画或动画 block (至少我是这么认为的)。

假设 UILabel 显示一些仪表值,例如“200 V”,并且此文本应更改为“400 V”。文本不应该只是从“200 V”跳到“400 V”,而是应该使用一些缓动函数来计算:“200 V”、“220 V”、“240 V”...“390 V”、“395” V” ... “400 V”

在 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 中是否有内置的东西可以解决这个问题,或者至少有方便的第三方实现可用?

非常感谢!

最佳答案

由于我找不到为此量身定制的解决方案,因此我创建了自己的解决方案:一个简单的 Animator 类来处理缓动:

// 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];
}

也许不是最漂亮的解决方案,但它确实有效:-)

关于ios - iOS 中的动画文本内容 - 相当于 Android ValueAnimator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25885545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com