【发布时间】:2023-03-03 22:16:01
【问题描述】:
我正在开发一个 iOS 7+ 应用程序,并希望以动画方式更改 UILabel 的内容。我不想做任何图形动画,例如淡出旧内容/淡入新内容。因此,iOS 提供的所有标准动画功能(例如图层动画或动画块)都无法使用(至少我认为如此)。
假设 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 中有没有内置的东西可以解决这个问题,或者至少有方便的第三方实现可用?
非常感谢!
【问题讨论】:
-
如果您已成功找到解决方案,请发布。
-
@TommieC。我使用了一个循环,但它不是很流畅,而且看起来很滞后。您找到解决方案了吗?
-
@JayVDiyk - 还没有找到简单的 iOS 解决方案,但我确实从 Core Animator 团队获得了这个示例:在终端窗口中从 bitbucket 克隆项目 ~ git clone mingsai@bitbucket.org/mingsai/odometer-sample.git
-
看看stackoverflow.com/questions/14192816/…,我认为它回答了你的问题。