指南 · 05

進場、離場與版面

保留離場的子元素直到動態結束,並為位置與尺寸的變化加上動畫,甚至跨越元件。

Animated.Keep

Animated.Keep 會保留有 key 的直接子元素,直到它們的 depart 姿態結束。seed={false} 讓首次渲染時已存在的子元素直接從其 pose 開始,wait 讓進場的子元素等到離場結束,onDeparted 則在最後一個離場後觸發。

正在準備範例…

Animated.Scene 與 move

Animated.Scene 會測量已提交的版面。具有 move 的元素會為其位置與尺寸的變化加上動畫;move="pill" 這樣的字串會在 Scene 中連結具有相同識別的元素,即使一個被卸載、另一個被掛載。

這些元素版面上的 translate 與 scale 由 Scene 掌管,所以請把你自己的 transform 放在巢狀元素上。

正在準備範例…

用 Hook 控制出現與消失

如果你偏好自己控制掛載,useAnimatedPresence(visible, { enter, exit }) 會回傳 { mounted, animatedValue }。

Toast.jsx
const { mounted, animatedValue } = useAnimatedPresence(open, {
  enter: (value) => spring(value, { toValue: 1 }),
  exit: (value) => timing(value, { toValue: 0, duration: 180 }),
});

return mounted ? <div style={{ opacity: animatedValue }}>Saved</div> : null;

在 React 之外

createLayoutScene 為任何渲染器或手動的 DOM 修改提供相同的擷取/提交協調。

outside-react.js
const scene = createLayoutScene({ tempo: { spring: { stiffness: 320, damping: 30 } } });
const card = scene.attach(element, { key: 'card' });

scene.capture(); // before the DOM changes
list.prepend(element); // any renderer or manual DOM work
scene.commit(); // animates from the captured rectangles

搜尋

輸入關鍵字搜尋文件。