คู่มือ · 03
คอมโพเนนต์ Animated
เรนเดอร์องค์ประกอบธรรมดาที่เคลื่อนไปยังปลายทาง ตอบสนองท่าทาง และจากไปอย่างนุ่มนวล
Animated.<tag>
Animated.div, Animated.button, Animated.svg และแท็ก HTML หรือ SVG อื่นทุกตัวเรนเดอร์องค์ประกอบธรรมดาโดยไม่มีตัวห่อ style ของมันรับค่าได้ และ transform เป็นอาร์เรย์ได้เหมือนใน React Native
การเคลื่อนไหว transform และ opacity ที่มีจุดสิ้นสุดบนองค์ประกอบเหล่านี้ รวมถึงสปริง เบราว์เซอร์เล่นเองได้
seed, pose, poses และ tempo
seed คือเฟรมแรก pose คือปลายทางปัจจุบัน (ออบเจกต์หรือชื่อรายการใน poses) และ tempo คือ timing หรือสปริง การเปลี่ยนแค่ tempo จะไม่เล่นปลายทางที่ไม่เปลี่ยนซ้ำ
onRest ทำงานเมื่อองค์ประกอบไปถึง
Card.jsx<Animated.article
seed={{ opacity: 0, transform: [{ translateY: 16 }] }}
pose={selected ? 'raised' : 'rest'}
poses={{
rest: { opacity: 1, transform: [{ translateY: 0 }, { scale: 1 }] },
raised: { opacity: 1, transform: [{ translateY: -8 }, { scale: 1.04 }] },
}}
tempo={{ spring: { stiffness: 300, damping: 26 } }}
onRest={() => console.log('arrived')}
/>;
ตอบสนองท่าทาง
respond จับคู่ท่าทางกับปลายทางชั่วคราว: hover, focus, focusWithin, press, longPress, drag, inView และอื่น ๆ เมื่อการตอบสนองจบ องค์ประกอบจะกลับสู่ pose ของตัวเองจากที่ใดก็ตามที่มันอยู่
onResponse รายงานจุดเริ่มต้นและจุดสิ้นสุดของทุกการตอบสนอง และ respond={{ press: {} }} ติดตามท่าทางโดยไม่มีการเปลี่ยนแปลงที่มองเห็น
แชร์ค่าเริ่มต้นด้วย Animated.Scope
Animated.Scope ส่งชื่อ pose, tempo และค่าเริ่มต้นของแอนิเมชันให้องค์ประกอบ Animated ที่อยู่ข้างใน
Menu.jsx<Animated.Scope tempo={{ spring: { stiffness: 380, damping: 30 } }}>
<Animated.li pose={{ opacity: 1 }} />
<Animated.li pose={{ opacity: 1 }} />
</Animated.Scope>;
คอมโพเนนต์ของคุณเอง
Animated.wrap(Component) — createAnimatedComponent ใน React Native — ทำให้คอมโพเนนต์ที่ส่งต่อ ref และกลายเป็นองค์ประกอบ DOM หนึ่งตัวรับ props เดียวกันได้
Link.jsximport { forwardRef } from 'react';
import { Animated } from '@kkwaenggwari/animated';
const Link = forwardRef((props, ref) => <a ref={ref} {...props} />);
const AnimatedLink = Animated.wrap(Link);
<AnimatedLink href="/pricing" respond={{ hover: { transform: [{ translateX: 4 }] } }} />;