指南 · 02

时序、弹簧与组合

选择驱动器、组合多个驱动器,并确切知道停止或替换时会发生什么。

timing、spring 和 decay

timing 在固定的 duration 内沿曲线运动。spring 遵循物理规律,没有固定时长。decay 带着初速度开始,然后逐渐减速。

同样的驱动器接受数字、ValueXY 或记录,所以 { x, y, opacity } 可以一起移动。

正在准备示例…

弹簧参数

只使用一种参数写法。都不指定时,使用 React Native 的默认弹簧(tension 40、friction 7)。弹簧的 velocity 默认取自被它打断的轨迹的速度。

springs.js
spring(value, { toValue: 1 }); // React Native default
spring(value, { toValue: 1, stiffness: 260, damping: 20 }); // physical
spring(value, { toValue: 1, bounciness: 8, speed: 12 }); // React Native style
spring(value, { toValue: 1, tension: 40, friction: 7 }); // origami style

组合

sequence 依次运行,parallel 同时运行,stagger 按延迟依次启动,delay 等待,loop 重复。结果仍然是控制器,所以组合可以嵌套。

正在准备示例…

start、stop 和 reset

每个控制器都有 start(callback?)、stop() 和 reset()。回调会收到 { finished }:动效被停止或替换时为 false。

在同一个值上启动新的驱动器,会从当前位置替换正在运行的那个,不会跳到任何一端。

controller.js
const move = timing(value, { toValue: 1, duration: 600 });

move.start(({ finished }) => {
  // finished is false when stopped or replaced by a new animation
});
move.stop(); // keeps the value where it is on screen
move.reset(); // returns to where this animation started

追随另一个值

动态的 toValue 会在那个值变化时重新启动驱动器,就像 React Native 中的跟踪。follow(target, source) 是同一种弹簧连接的具名函数形式。

tracking.js
// A live toValue tracks another value, like React Native tracking.
spring(badge, { toValue: pointer, stiffness: 300, damping: 26 }).start();

// follow() is the same connection as a named function.
follow(badge, pointer, { stiffness: 300, damping: 26 });

缓动与关键帧

Easing 与 React Native 一致:linear、quad、cubic、sin、circle、exp、elastic、back、bounce、bezier,以及 in、out、inOut 辅助函数。keyframes(value, frames, options) 让一个值依次经过多个数字,可选 times 和 repeat。

搜索

输入关键词搜索文档。