Lesson 9 of 9
Practice & Quiz - Animations & Gestures
Test your mastery of Reanimated 3 worklets, Gesture Handler swipe-to-dismiss, shared values, and layout animations with a practical exercise and 5 quiz questions.
Practical Exercise - Swipe-to-Dismiss Card with Reanimated 3
import Animated, {
useSharedValue, useAnimatedStyle, useAnimatedGestureHandler,
withSpring, withTiming, runOnJS, interpolate, Extrapolation,
} from 'react-native-reanimated';
import { PanGestureHandler } from 'react-native-gesture-handler';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
const { width: SCREEN_W } = Dimensions.get('window');
const SWIPE_THRESHOLD = SCREEN_W * 0.4;
interface SwipeCardProps {
title: string;
onDismiss: (direction: 'left' | 'right') => void;
}
export function SwipeCard({ title, onDismiss }: SwipeCardProps) {
// Shared values live on the UI thread - no bridge crossing
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
// All animation logic runs as a worklet (UI thread)
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx: { startX: number }) => {
ctx.startX = translateX.value;
},
onActive: (event, ctx) => {
translateX.value = ctx.startX + event.translationX;
// Slight vertical drift for natural feel
translateY.value = Math.abs(translateX.value) * 0.05;
},
onEnd: (event) => {
const shouldDismiss = Math.abs(event.translationX) > SWIPE_THRESHOLD;
if (shouldDismiss) {
const direction = event.translationX > 0 ? 'right' : 'left';
translateX.value = withTiming(
event.translationX > 0 ? SCREEN_W * 1.5 : -SCREEN_W * 1.5,
{ duration: 300 },
// runOnJS: call JS function from UI thread worklet
(finished) => {
if (finished) runOnJS(onDismiss)(direction);
}
);
} else {
// Snap back with spring physics
translateX.value = withSpring(0, { damping: 20, stiffness: 300 });
translateY.value = withSpring(0);
}
},
});
const animatedStyle = useAnimatedStyle(() => {
// Rotate based on swipe distance - natural card feel
const rotate = interpolate(
translateX.value,
[-SCREEN_W / 2, 0, SCREEN_W / 2],
[-15, 0, 15],
Extrapolation.CLAMP
);
return {
transform: [
{ translateX: translateX.value },
{ translateY: translateY.value },
{ rotate: `${rotate}deg` },
],
};
});
// Background color hint
const overlayStyle = useAnimatedStyle(() => ({
backgroundColor: translateX.value > 0 ? '#22c55e' : '#ef4444',
opacity: interpolate(Math.abs(translateX.value), [0, SWIPE_THRESHOLD], [0, 0.7], Extrapolation.CLAMP),
...StyleSheet.absoluteFillObject,
borderRadius: 16,
}));
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.card, animatedStyle]}>
<Animated.View style={overlayStyle} />
<Text style={styles.cardText}>{title}</Text>
<Text style={styles.hint}>โ Swipe left to dismiss ยท Swipe right to save โ</Text>
</Animated.View>
</PanGestureHandler>
);
}
const styles = StyleSheet.create({
card: { width: SCREEN_W - 48, height: 280, backgroundColor: '#fff', borderRadius: 16, padding: 24, justifyContent: 'center', alignSelf: 'center', elevation: 8, shadowColor: '#000', shadowOffset: { width: 0, height: 4 }, shadowOpacity: 0.15, shadowRadius: 12 },
cardText: { fontSize: 20, fontWeight: '700', color: '#1a1a1a', marginBottom: 8 },
hint: { fontSize: 12, color: '#999', textAlign: 'center' },
});Module Quiz
What is a Reanimated 3 'worklet' and why does it enable smooth animations?
Tip
Tip
Practice Practice Quiz Animations Gestures in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Module 10 Knowledge Check: Reanimated 3, Gesture Handler, Lottie, and scroll interpolation
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz Animations Gestures from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz Animations Gestures is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?