0Pricing
React Native Academy · บทเรียน

การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ

ประกอบแอนิเมชันด้วย Animated.parallel เพื่อให้ทำงานพร้อมกัน และ Animated.sequence เพื่อเชื่อมให้ทำงานต่อกันทีละรายการสำหรับแอนิเมชันการปรากฏที่ซับซ้อน

การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ เป็นบทเรียน React Native Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน React Native Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Composing Complex Animations

Real UI animations are rarely a single value changing in isolation. A modal might slide up and fade in at the same time. A success icon might bounce in, then a label slides in after. React Native's Animated API provides composition helpers that let you orchestrate multiple animations working together: Animated.parallel, Animated.sequence, Animated.stagger, and Animated.delay.

These composition methods return animation objects that can be started, stopped, and even nested inside each other for complex multi-step effects.

Animated.parallel: Simultaneous Animations

Animated.parallel takes an array of animations and runs them all at the same time. When all animations in the array complete, the parallel group is done. This is the right choice when multiple properties should change together — for example, fading in while sliding up.

By default, if one animation in a parallel group stops (e.g., due to an interruption), all others stop too. You can override this with { stopTogether: false } as the second argument.

const opacity = useRef(new Animated.Value(0)).current;
const translateY = useRef(new Animated.Value(50)).current;

Animated.parallel([
  Animated.timing(opacity, {
    toValue: 1,
    duration: 400,
    useNativeDriver: true,
  }),
  Animated.timing(translateY, {
    toValue: 0,
    duration: 400,
    useNativeDriver: true,
  }),
]).start();

Animated.sequence: One After Another

Animated.sequence takes an array of animations and runs them one at a time — the next animation only starts when the previous one completes. This is perfect for multi-step choreography: first a button presses in, then a spinner appears, then a checkmark pops in.

If any animation in a sequence is stopped, the subsequent animations will not run. Sequences can be nested inside parallel groups and vice versa for complex choreography.

const scale = useRef(new Animated.Value(1)).current;
const checkOpacity = useRef(new Animated.Value(0)).current;

Animated.sequence([
  // Step 1: button shrinks
  Animated.spring(scale, { toValue: 0.9, useNativeDriver: true }),
  // Step 2: wait 300ms
  Animated.delay(300),
  // Step 3: checkmark appears
  Animated.timing(checkOpacity, {
    toValue: 1,
    duration: 200,
    useNativeDriver: true,
  }),
]).start();

Animated.delay: Pausing Between Steps

Animated.delay does nothing for a specified number of milliseconds. It is only useful inside a sequence, creating a pause between animation steps. Using delay you can build natural pacing into multi-step animations.

For example, you might show a success message with a brief delay after an action completes, giving the user time to register what happened before the UI changes. Delays under 300ms are usually imperceptible; 400-800ms feels deliberate and intentional.

Animated.sequence([
  Animated.timing(opacity, { toValue: 1, duration: 300, useNativeDriver: true }),
  Animated.delay(500), // pause for half a second
  Animated.timing(opacity, { toValue: 0, duration: 300, useNativeDriver: true }),
]).start();

Animated.stagger: Cascading Effects

Animated.stagger is like parallel but with a time offset between starting each animation. The first animation starts immediately, the second starts after the stagger delay, the third after 2× the delay, and so on. This creates beautiful cascading entrance effects for lists or menus.

Stagger is perfect for animating list items entering the screen — each item fades/slides in slightly after the previous, giving the eye a clear path to follow through the content.

const items = [0, 1, 2, 3];
const opacities = items.map(() => new Animated.Value(0));

Animated.stagger(
  100, // 100ms between each start
  opacities.map(opacity =>
    Animated.timing(opacity, {
      toValue: 1,
      duration: 300,
      useNativeDriver: true,
    })
  )
).start();

Building a Multi-Step Modal Entry

A polished modal entrance might: (1) the overlay fades in, (2) the modal card slides up and fades in simultaneously, (3) the title text appears. This three-phase choreography uses a sequence where the first step is the overlay, the second step is a parallel animation for the card, and the third step is the title.

Wrapping this in a useEffect that fires when the modal becomes visible gives you the complete entrance animation triggered by state.

const overlayOpacity = useRef(new Animated.Value(0)).current;
const cardTranslateY = useRef(new Animated.Value(80)).current;
const cardOpacity = useRef(new Animated.Value(0)).current;

useEffect(() => {
  if (visible) {
    Animated.sequence([
      Animated.timing(overlayOpacity, { toValue: 0.5, duration: 200, useNativeDriver: true }),
      Animated.parallel([
        Animated.timing(cardOpacity, { toValue: 1, duration: 300, useNativeDriver: true }),
        Animated.spring(cardTranslateY, { toValue: 0, useNativeDriver: true }),
      ]),
    ]).start();
  }
}, [visible]);

Building a Modal Exit Animation

Exit animations run in reverse order: first the card slides away and fades out, then the overlay fades out, then you update state to unmount the modal. Trigger the exit in the close handler, wait for the animation to complete in the callback, and only then call setVisible(false).

This prevents the modal from abruptly disappearing before the animation finishes — a common mistake that makes apps feel cheap.

function closeModal() {
  Animated.sequence([
    Animated.parallel([
      Animated.timing(cardOpacity, { toValue: 0, duration: 200, useNativeDriver: true }),
      Animated.timing(cardTranslateY, { toValue: 80, duration: 200, useNativeDriver: true }),
    ]),
    Animated.timing(overlayOpacity, { toValue: 0, duration: 150, useNativeDriver: true }),
  ]).start(() => setVisible(false)); // unmount AFTER animation
}

Nested Parallel Inside Sequence

Composition methods can be nested freely. A parallel inside a sequence means that at that step, multiple things happen simultaneously. A sequence inside a parallel means one branch runs step-by-step while another branch runs immediately in parallel.

Think of it like planning a theatre production: some actors are on stage at once (parallel), some scenes happen back to back (sequence), and some special effects happen with a ripple delay (stagger).

// Sequence with a parallel step inside
Animated.sequence([
  Animated.timing(step1, { toValue: 1, duration: 300, useNativeDriver: true }),
  Animated.parallel([
    Animated.timing(step2a, { toValue: 1, duration: 400, useNativeDriver: true }),
    Animated.spring(step2b, { toValue: 1, useNativeDriver: true }),
  ]),
  Animated.timing(step3, { toValue: 1, duration: 200, useNativeDriver: true }),
]).start();

Stopping and Resetting Composed Animations

The object returned by composition methods has a .stop() method. Store the animation in a ref so you can stop it from outside (such as when a component unmounts mid-animation or when a new animation should interrupt the current one).

Always stop animations in the useEffect cleanup to prevent 'setState on unmounted component' warnings and memory leaks, especially for looping or long-running sequences.

const animRef = useRef(null);

useEffect(() => {
  animRef.current = Animated.sequence([
    Animated.timing(a, { toValue: 1, duration: 500, useNativeDriver: true }),
    Animated.timing(b, { toValue: 1, duration: 500, useNativeDriver: true }),
  ]);
  animRef.current.start();

  return () => {
    if (animRef.current) animRef.current.stop();
  };
}, []);

List Item Stagger on Screen Entry

A practical stagger implementation initializes an array of Animated.Values (one per list item) all at opacity 0, then runs a stagger animation in a useEffect when data loads. Each item in the rendered list connects to its animated value via index.

This technique makes content-heavy screens feel more dynamic. Users perceive staggered loading as faster than everything appearing at once, even though the total time is longer.

const itemAnimations = useRef(data.map(() => new Animated.Value(0))).current;

useEffect(() => {
  Animated.stagger(
    80,
    itemAnimations.map(anim =>
      Animated.timing(anim, { toValue: 1, duration: 350, useNativeDriver: true })
    )
  ).start();
}, [data]);

// In renderItem:
<Animated.View style={{ opacity: itemAnimations[index] }}>
  <ItemComponent item={item} />
</Animated.View>

Coordinating With Component Lifecycle

Composing animations with the component lifecycle ensures animations play at the right moment. Use useEffect for mount animations, state change effects for conditional animations, and callback composition for exit animations before unmounting.

Keep animation logic close to the component that owns the animated values. If animation logic becomes complex, extract it into a custom hook that returns the animated values and trigger functions, keeping the JSX layer clean.

// Custom hook pattern
function useFadeSlide() {
  const opacity = useRef(new Animated.Value(0)).current;
  const translateY = useRef(new Animated.Value(20)).current;

  const animateIn = () =>
    Animated.parallel([
      Animated.timing(opacity, { toValue: 1, duration: 300, useNativeDriver: true }),
      Animated.timing(translateY, { toValue: 0, duration: 300, useNativeDriver: true }),
    ]);

  return { opacity, translateY, animateIn };
}

Quick Check

Test your understanding of React Native Mobile Development concepts from this lesson.

Lesson Recap

In this lesson you learned: Animated.parallel runs multiple animations simultaneously, Animated.sequence chains animations so each starts after the previous completes, and Animated.stagger creates cascading effects by offsetting start times across an array of animations. Next up we explore useNativeDriver to achieve 60fps animations by offloading work to the native thread.

คำถามที่พบบ่อย

บทเรียน “การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส React Native Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ”

ประกอบแอนิเมชันด้วย Animated.parallel เพื่อให้ทำงานพร้อมกัน และ Animated.sequence เพื่อเชื่อมให้ทำงานต่อกันทีละรายการสำหรับแอนิเมชันการปรากฏที่ซับซ้อน คุณปฏิบัติ React Native Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน React Native Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน React Native Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน React Native Academy นี้ได้ไหม

ได้ บทเรียน React Native Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Animated.Value และ Animated.View
  2. แอนิเมชันแบบสปริงและการลดความเร็ว
  3. การทำหลายคุณสมบัติให้เคลื่อนไหวแบบขนานและแบบลำดับ
  4. ใช้ useNativeDriver สำหรับแอนิเมชัน 60fps
← กลับไปที่ React Native Academy