π¬ React Native Animation Patterns
The Magic of Making Things Move
Imagine youβre a puppet master. Your strings can make puppets slide, fade, grow, and dance. In React Native, animation patterns are your invisible strings. You pull them, and your appβs elements come alive!
π― What Weβll Learn
Think of these as different dance moves for your app:
- Transform Animations β Moving & rotating things
- Opacity Animations β Fading in & out
- Scale Animations β Growing & shrinking
- Easing Functions β Making movement feel natural
- Common Animation Patterns β Popular dance combos
- Loading Animations β Keeping users entertained while waiting
1οΈβ£ Transform Animations
The Moving Truck Analogy π
A transform is like telling a moving truck where to go. You can say:
- βGo left 50 stepsβ (translateX)
- βGo up 30 stepsβ (translateY)
- βSpin around!β (rotate)
// Move right by 100 pixels
transform: [
{ translateX: 100 }
]
Types of Transforms
| Transform | What It Does | Real Example |
|---|---|---|
translateX |
Move left/right | Slide-in menu |
translateY |
Move up/down | Pull-to-refresh |
rotate |
Spin around | Loading spinner |
Simple Code Example
// Create animated value
const slideAnim = useRef(
new Animated.Value(0)
).current;
// Animate it!
Animated.timing(slideAnim, {
toValue: 100,
duration: 500,
useNativeDriver: true,
}).start();
// Use in style
<Animated.View style={{
transform: [
{ translateX: slideAnim }
]
}} />
Key Point: useNativeDriver: true makes animations super smooth because the phoneβs graphics chip does the work!
2οΈβ£ Opacity Animations
The Light Dimmer Analogy π‘
Opacity is like a dimmer switch for lights:
opacity: 1= Lights fully ON (100% visible)opacity: 0= Lights OFF (invisible)opacity: 0.5= Half bright (50% transparent)
graph TD A["opacity: 0"] --> B["Invisible"] C["opacity: 0.5"] --> D["Half visible"] E["opacity: 1"] --> F["Fully visible"]
Fade In Example
const fadeAnim = useRef(
new Animated.Value(0)
).current;
// Fade from invisible to visible
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start();
<Animated.View style={{
opacity: fadeAnim
}}>
<Text>I appear slowly!</Text>
</Animated.View>
When to Use Opacity
- β Fade in new content
- β Fade out deleted items
- β Cross-fade between images
- β Show/hide modals & popups
3οΈβ£ Scale Animations
The Balloon Analogy π
Scaling is like blowing up a balloon:
scale: 0= No balloon (invisible)scale: 1= Normal sizescale: 2= Twice as big!scale: 0.5= Half the size
Growing Button Example
const scaleAnim = useRef(
new Animated.Value(1)
).current;
// Make it grow when pressed
const onPressIn = () => {
Animated.spring(scaleAnim, {
toValue: 1.2,
useNativeDriver: true,
}).start();
};
// Return to normal
const onPressOut = () => {
Animated.spring(scaleAnim, {
toValue: 1,
useNativeDriver: true,
}).start();
};
<Animated.View style={{
transform: [{ scale: scaleAnim }]
}}>
<TouchableOpacity
onPressIn={onPressIn}
onPressOut={onPressOut}
>
<Text>Press Me!</Text>
</TouchableOpacity>
</Animated.View>
Scale Types
| Scale Type | Example | Use Case |
|---|---|---|
scale |
Both X and Y | Button press |
scaleX |
Width only | Progress bar |
scaleY |
Height only | Bar chart |
4οΈβ£ Easing Functions
The Skateboard Ramp Analogy πΉ
Imagine a skateboarder:
- Linear: Same speed the whole way (boring! π΄)
- Ease-in: Starts slow, speeds up (going down a ramp)
- Ease-out: Starts fast, slows down (going up a ramp)
- Ease-in-out: Slow β Fast β Slow (smooth ride!)
graph TD A["π Start"] --> B{Easing Type?} B --> C["Linear: βββββββ"] B --> D["Ease-In: β±"] B --> E["Ease-Out: β²"] B --> F["Ease-In-Out: βΏ"]
Using Easing
import { Easing } from 'react-native';
// Bouncy like a ball!
Animated.timing(anim, {
toValue: 1,
duration: 500,
easing: Easing.bounce,
useNativeDriver: true,
}).start();
Common Easing Functions
| Function | Feels Like | Best For |
|---|---|---|
Easing.linear |
Robot movement | Progress bars |
Easing.ease |
Natural motion | Most animations |
Easing.bounce |
Bouncy ball | Fun feedback |
Easing.elastic |
Rubber band | Playful UI |
Easing.back |
Pulls back first | Dramatic entrance |
Pro Tip π‘
// Combine easings!
Easing.bezier(0.25, 0.1, 0.25, 1)
// Custom curve - like designing
// your own skateboard ramp!
5οΈβ£ Common Animation Patterns
The Dance Move Library π
Just like dancers have standard moves, apps have common animation patterns:
Pattern 1: Fade + Slide (The Entrance)
// Slide up while fading in
const fadeAnim = useRef(
new Animated.Value(0)
).current;
const slideAnim = useRef(
new Animated.Value(50)
).current;
Animated.parallel([
Animated.timing(fadeAnim, {
toValue: 1,
duration: 300,
useNativeDriver: true,
}),
Animated.timing(slideAnim, {
toValue: 0,
duration: 300,
useNativeDriver: true,
}),
]).start();
<Animated.View style={{
opacity: fadeAnim,
transform: [{ translateY: slideAnim }]
}} />
Pattern 2: Spring Button
// Bouncy button press
Animated.spring(scaleAnim, {
toValue: 0.95,
friction: 3,
tension: 100,
useNativeDriver: true,
}).start();
Pattern 3: Staggered List
// Items appear one after another
const animations = items.map(
(_, i) => Animated.timing(
anims[i], {
toValue: 1,
duration: 200,
useNativeDriver: true,
}
)
);
Animated.stagger(100, animations)
.start();
Animation Composition
graph TD A["parallel"] --> B["All at once"] C["sequence"] --> D["One after another"] E["stagger"] --> F["Delayed starts"]
6οΈβ£ Loading Animations
The Entertainment While Waiting Analogy πͺ
Loading animations are like circus performers entertaining the crowd while the main show prepares!
Spinning Loader
const spinAnim = useRef(
new Animated.Value(0)
).current;
// Spin forever!
Animated.loop(
Animated.timing(spinAnim, {
toValue: 1,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
})
).start();
const spin = spinAnim.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg']
});
<Animated.View style={{
transform: [{ rotate: spin }]
}}>
<LoadingIcon />
</Animated.View>
Pulsing Dot
const pulseAnim = useRef(
new Animated.Value(1)
).current;
Animated.loop(
Animated.sequence([
Animated.timing(pulseAnim, {
toValue: 1.3,
duration: 500,
useNativeDriver: true,
}),
Animated.timing(pulseAnim, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}),
])
).start();
Three Bouncing Dots
// Each dot bounces with delay
const dots = [0, 1, 2].map(() =>
useRef(new Animated.Value(0)).current
);
const bounce = (anim, delay) => {
Animated.loop(
Animated.sequence([
Animated.delay(delay),
Animated.timing(anim, {
toValue: -10,
duration: 300,
useNativeDriver: true,
}),
Animated.timing(anim, {
toValue: 0,
duration: 300,
useNativeDriver: true,
}),
])
).start();
};
// Start each with staggered delay
dots.forEach((anim, i) =>
bounce(anim, i * 150)
);
Popular Loading Patterns
| Pattern | Visual | Best For |
|---|---|---|
| Spinner | π | Universal loading |
| Pulse | π | Waiting for data |
| Skeleton | βββ | Content placeholders |
| Dots | β β β | Chat typing indicator |
| Progress | βββββββ | Known duration |
π― Quick Reference
graph TD A["Animation Type"] --> B["Transform"] A --> C["Opacity"] A --> D["Scale"] B --> E["translateX/Y"] B --> F["rotate"] C --> G["0 to 1"] D --> H["scale"] D --> I["scaleX/Y"]
π Key Takeaways
- Transform = Moving & rotating (like a truck)
- Opacity = Fading (like a dimmer)
- Scale = Growing & shrinking (like a balloon)
- Easing = Making movement feel natural (like a skateboard ramp)
- Patterns = Combined moves (like dance choreography)
- Loading = Entertainment while waiting (like a circus act)
Remember: Always use useNativeDriver: true for smooth 60fps animations!
Youβve learned the puppet masterβs secrets! Now go make your apps dance! π
