Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

animate on scroll using reanimate lib #1

Merged
merged 1 commit into from
Sep 15, 2024
Merged

animate on scroll using reanimate lib #1

merged 1 commit into from
Sep 15, 2024

Conversation

jittuu
Copy link
Owner

@jittuu jittuu commented Sep 15, 2024

Overview

Animated using Reanimated Lib on FlashList's scroll,

// ..
// other imports here...
// ..

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

// create AnimatedFlashList to work with reanimated lib
const AnimatedFlashList = Animated.createAnimatedComponent(FlashList); 

export default function ReanimatedFlashListScreen() {
  const scrollY = useSharedValue(0);
  // ..
  // other code here...
  // ..


  const scrollHandler = useAnimatedScrollHandler({
    onScroll: event => {
      scrollY.value = event.contentOffset.y; // bind scroll offset to shared value
    },
  });

  // animate using share value
  const headerAnimatedStyle = useAnimatedStyle(() => {
    return {
      opacity: interpolate(
        scrollY.value,
        [0, 100],
        [1, 0],
        Extrapolation.CLAMP,
      ),
      transform: [
        {
          scale: interpolate(
            scrollY.value,
            [-100, 0, 100],
            [3, 1, 0],
            Extrapolation.CLAMP,
          ),
        },
      ],
    };
  });
  // ..
  // other code here...
  // ..

  return (
    <>
      <Stack.Screen options={{ title: 'Animate onScroll' }} />
      <ThemedView style={styles.container}>
        <AnimatedFlashList
          data={names}
          renderItem={({ item }) => (
            <View style={styles.item}>
              <Text>{item as string}</Text>
            </View>
          )}
          estimatedItemSize={50}
          ListHeaderComponent={renderParallaxHeader}
          onScroll={scrollHandler}  // <-- scrollHandler
          scrollEventThrottle={16}
        />
      </ThemedView>
    </>
  );
}

Bugs

Code is broken as of Reanimated package 3.10.1 - which is recommended version of expo 51. We have to upgrade to 3.15.2 to work with onScroll event handler.

@jittuu jittuu merged commit 5b651ed into main Sep 15, 2024
@jittuu jittuu deleted the animate-on-scroll branch September 15, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant