Spring Animation

SpringAnimation自iOS7以来被广泛应用于各种系统动画中,如App文件夹的开合效果、UISwitch的开关效果等。本文介绍了SpringAnimation的特点及其实现方式,并通过实例展示了其与Ease-OutAnimation和LinearAnimation的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Animation 是一种特殊的动画曲线,自从 iOS 7 开始被广泛应用在系统动画中。

下图中演示的系统自带的动画效果,都使用了 Spring Animation:

0-example-2  0-example-1

事实上,从 iOS 7 起几乎所有的系统动画都用的是 Spring Animation,包括 App 文件夹打开/关闭效果、键盘弹出效果、UISwitch 控件的开关效果、不同 View Controller 之间的 Push 动画、Modal 出现和消失的动画、Siri 的出现和消失动画,等等。下图为 Spring Animation 和普通的动画的运动曲线的对比:

cmp

为了更加直观,我做了一组演示图,从左至右分别列出了 Spring Animation, Ease-Out Animation 和 Linear Animation 的动画效果:

spring-1

可以看到,和系统自带的 ease-out 效果相比,Spring Animation 前期速度增加得更快,在动画时间一定的前提下,给人感觉更加快速、干净。

Spring Animation API

自 iOS 8 开始,Apple 公开了 Spring Animation 的 API,开发者也可以使用简单的代码创建这类动画效果了:

+ (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay
     usingSpringWithDamping:(CGFloat)dampingRatio
      initialSpringVelocity:(CGFloat)velocity
                    options:(UIViewAnimationOptions)options
                 animations:(void (^)(void))animations
                 completion:(void (^)(BOOL finished))completion

该方法是UIView的类方法。

Spring Animation 的 API 和一般动画相比多了两个参数,分别是usingSpringWithDampinginitialSpringVelocity

usingSpringWithDamping 参数

usingSpringWithDamping的范围为0.0f1.0f,数值越小「弹簧」的振动效果越明显。下图演示了在initialSpringVelocity0.0f的情况下,usingSpringWithDamping分别取0.2f0.5f1.0f的情况。

spring-2

initialSpringVelocity 参数

initialSpringVelocity则表示初始的速度,数值越大一开始移动越快。下图演示了在usingSpringWithDamping1.0f时,initialSpringVelocity分别取5.0f15.0f25.0f的情况。值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。

spring-3

使用

Spring Animation 是线性动画或 ease-out 动画的理想替代品。由于 iOS 本身大量使用的就是 Spring Animation,用户已经习惯了这种动画效果,因此使用它能使 App 让人感觉更加自然,用 Apple 的话说就是「instantly familiar」。此外,Spring Animation 不只能对位置使用,它适用于所有可被添加动画效果的属性。

declare class Scroller { /** * constructor. * * @syscap SystemCapability.ArkUI.ArkUI.Full * @since 7 */ /** * constructor. * * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @since 10 */ /** * constructor. * * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @atomicservice * @since 11 */ constructor(); /** * Called when the setting slides to the specified position. * * @param { object } value * @syscap SystemCapability.ArkUI.ArkUI.Full * @since 7 */ /** * Called when the setting slides to the specified position. * * @param { object } value * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @since 10 */ /** * Called when the setting slides to the specified position. * * @param { object } value * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @atomicservice * @since 11 */ scrollTo(value: { /** * The X-axis offset. * * @type { number | string } * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @since 10 */ /** * The X-axis offset. * * @type { number | string } * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @atomicservice * @since 11 */ xOffset: number | string; /** * The Y-axis offset. * * @type { number | string } * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @since 10 */ /** * The Y-axis offset. * * @type { number | string } * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @atomicservice * @since 11 */ yOffset: number | string; /** * Descriptive animation. * * @type { ?({ duration?: number; curve?: Curve | ICurve } | boolean) } The object type provides custom animation parameters * and the boolean type enables default spring animation. * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @since 10 */ /** * Descriptive animation. * * @type { ?({ duration?: number; curve?: Curve | ICurve } | boolean) } The object type provides custom animation parameters * and the boolean type enables default spring animation. * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @atomicservice * @since 11 */ /** * Descriptive animation. * * @type { ?( ScrollAnimationOptions | boolean) } The ScrollAnimationOptions type provides custom animation parameters * and the boolean type enables default spring animation. * @syscap SystemCapability.ArkUI.ArkUI.Full * @crossplatform * @atomicservice * @since 12 */ animation?: ScrollAnimationOptions | boolean; });
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值