指南 · 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。

搜尋

輸入關鍵字搜尋文件。