Skip to main content

Lesson 6 of 9

Interpolation - Connecting Scroll to UI

Interpolation maps one range of values to another. Its power is turning a single scroll position (scrollY = 0 to 300) into many simultaneous animations: header fades, tab bar hides, icon scales, background color changes - all derived from the same shared value.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Interpolation Patterns

Interpolation Patterns
import { interpolate, Extrapolation, useSharedValue, useAnimatedStyle, useAnimatedScrollHandler } from 'react-native-reanimated';

const HEADER_HEIGHT = 56;
const COLLAPSE_START = 0;
const COLLAPSE_END = 120; // px of scroll to fully collapse

function AnimatedHeader() {
  const scrollY = useSharedValue(0);

  const scrollHandler = useAnimatedScrollHandler(e => {
    scrollY.value = e.contentOffset.y;
  });

  // Header collapses from 120 to 56px
  const headerHeightStyle = useAnimatedStyle(() => ({
    height: interpolate(
      scrollY.value,
      [COLLAPSE_START, COLLAPSE_END],
      [120, HEADER_HEIGHT],
      Extrapolation.CLAMP // never go below 56 or above 120
    ),
  }));

  // Avatar fades out
  const avatarStyle = useAnimatedStyle(() => ({
    opacity: interpolate(scrollY.value, [0, 60], [1, 0], Extrapolation.CLAMP),
    transform: [{
      scale: interpolate(scrollY.value, [0, 100], [1, 0.5], Extrapolation.CLAMP),
    }],
  }));

  // Background blurs in (simulated by opacity of overlay)
  const blurOverlayStyle = useAnimatedStyle(() => ({
    opacity: interpolate(scrollY.value, [60, COLLAPSE_END], [0, 1], Extrapolation.CLAMP),
  }));

  // Tab bar slides down and hides when scrolling down, comes back on scroll up
  const prevScrollY = useSharedValue(0);
  const tabBarTranslateY = useSharedValue(0);

  const tabBarScrollHandler = useAnimatedScrollHandler({
    onScroll: e => {
      const delta = e.contentOffset.y - prevScrollY.value;
      prevScrollY.value = e.contentOffset.y;
      tabBarTranslateY.value = Math.max(0, Math.min(80, tabBarTranslateY.value + delta));
    },
  });

  const tabBarStyle = useAnimatedStyle(() => ({
    transform: [{ translateY: tabBarTranslateY.value }],
  }));

  return (
    <View style={{ flex: 1 }}>
      <Animated.View style={[{ backgroundColor: '#0f172a', overflow: 'hidden', position: 'absolute', top: 0, left: 0, right: 0, zIndex: 10 }, headerHeightStyle]}>
        <Animated.View style={[{ position: 'absolute', inset: 0, backgroundColor: '#1e293b99' }, blurOverlayStyle]} />
        <Animated.View style={[{ width: 60, height: 60, borderRadius: 30, backgroundColor: '#229c6b', position: 'absolute', bottom: 8, left: 20 }, avatarStyle]} />
      </Animated.View>

      <Animated.ScrollView onScroll={tabBarScrollHandler} scrollEventThrottle={16} contentContainerStyle={{ paddingTop: 120, padding: 16 }}>
        {Array.from({ length: 20 }, (_, i) => (
          <View key={i} style={{ height: 80, backgroundColor: '#1e293b', borderRadius: 10, marginBottom: 8 }} />
        ))}
      </Animated.ScrollView>

      {/* Hiding tab bar */}
      <Animated.View style={[{ position: 'absolute', bottom: 0, left: 0, right: 0, height: 80, backgroundColor: '#0f172a', borderTopWidth: 1, borderTopColor: '#1e293b' }, tabBarStyle]} />
    </View>
  );
}

Tip

Tip

Practice Interpolation Connecting Scroll to UI in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagramโ€ฆ

Building dynamic collapsing headers and sticky elements mapped to scroll offset

Practice Task

Note

Practice Task - (1) Write a working example of Interpolation Connecting Scroll to UI 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.

Quick Quiz

Common Mistake

Warning

A common mistake with Interpolation Connecting Scroll to UI 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.

Topics in This Module

0/9 done