跳至主要内容

Easing

Easing 模块实现了常用的缓动函数。这个模块被 Animated.timing() 用于在动画中传达逼真的物理运动。

你可以在 https://easings.net/ 上找到一些常用缓动函数的图示。

预定义动画

Easing 模块通过以下方法提供了几个预定义动画

  • back 提供了一个基础动画,其中对象在向前移动之前稍微向后移动
  • bounce 提供了一个弹跳动画
  • ease 提供了一个基础的惯性动画
  • elastic 提供了一个基础的弹簧效果

标准函数

提供了三种标准缓动函数

poly 函数可用于实现四次、五次及其他高次函数。

附加函数

通过以下方法提供了附加的数学函数

  • bezier 提供了一个三次贝塞尔曲线
  • circle 提供了一个圆函数
  • sin 提供了一个正弦函数
  • exp 提供了一个指数函数

以下辅助函数用于修改其他缓动函数。

  • in 正向运行一个缓动函数
  • inOut 使任何缓动函数对称
  • out 反向运行一个缓动函数

示例


参考

方法

step0()

tsx
static step0(n: number);

阶跃函数,对于 n 的任何正值都返回 1。


step1()

tsx
static step1(n: number);

阶跃函数,如果 n 大于或等于 1 则返回 1。


linear()

tsx
static linear(t: number);

线性函数,f(t) = t。位置与经过的时间一一对应。

https://cubic-bezier.com/#0,0,1,1


ease()

tsx
static ease(t: number);

基础的惯性交互,类似于物体缓慢加速到一定速度。

https://cubic-bezier.com/#.42,0,1,1


quad()

tsx
static quad(t: number);

二次函数,f(t) = t * t。位置等于经过时间的平方。

https://easings.net/#easeInQuad


cubic()

tsx
static cubic(t: number);

三次函数,f(t) = t * t * t。位置等于经过时间的立方。

https://easings.net/#easeInCubic


poly()

tsx
static poly(n: number);

幂函数。位置等于经过时间的 N 次幂。

n = 4: https://easings.net/#easeInQuart n = 5: https://easings.net/#easeInQuint


sin()

tsx
static sin(t: number);

正弦函数。

https://easings.net/#easeInSine


circle()

tsx
static circle(t: number);

圆函数。

https://easings.net/#easeInCirc


exp()

tsx
static exp(t: number);

指数函数。

https://easings.net/#easeInExpo


elastic()

tsx
static elastic(bounciness: number);

基础的弹性交互,类似于弹簧来回振荡。

默认的弹跳度为 1,这会轻微地回弹一次。弹跳度为 0 不会回弹,弹跳度 N > 1 会回弹约 N 次。

https://easings.net/#easeInElastic


back()

tsx
static back(s)

Animated.parallel() 一起使用,以创建一种基础效果,即对象在动画开始时稍微向后移动。


bounce()

tsx
static bounce(t: number);

提供基础的弹跳效果。

https://easings.net/#easeInBounce


bezier()

tsx
static bezier(x1: number, y1: number, x2: number, y2: number);

提供三次贝塞尔曲线,相当于 CSS Transitions 的 transition-timing-function

可以在 https://cubic-bezier.com/ 找到一个可视化三次贝塞尔曲线的有用工具。


in()

tsx
static in(easing: number);

正向运行一个缓动函数。


out()

tsx
static out(easing: number);

反向运行一个缓动函数。


inOut()

tsx
static inOut(easing: number);

使任何缓动函数对称。缓动函数会在一半持续时间正向运行,然后在剩余持续时间反向运行。