Добрый день. Подскажите пожалуйста как можно сделать header который скрывается при скроле вниз, и раскрывается при скроле вверх, допустим как в инстаграмме.
основная логика
const [headerHeight, setHeaderHeight] = useState(0);
const scrollYRef = useRef(0);
const top = useSharedValue(0);
const headerStyle = useAnimatedStyle(() => ({
transform: [{ translateY: top.value }],
}));
const onLayoutHeader = (e: LayoutChangeEvent) =>
setHeaderHeight(e.nativeEvent.layout.height);
const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => {
const y = e.nativeEvent.contentOffset.y;
const scrollingDown = y > scrollYRef.current;
if (scrollingDown && y > headerHeight) {
top.value = withTiming(-headerHeight, { duration: 500 });
} else if (!scrollingDown) {
top.value = withTiming(0, { duration: 500 });
}
scrollYRef.current = y;
};
шапочка
<Animated.View
onLayout={onLayoutHeader}
style={[styles.header, headerStyle]}
>
<View style={styles.flex}>
<InputSearchAdsList />
</View>
<ButtonFilters />
</Animated.View>
список
<FlashList
data={[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20,
]}
renderItem={renderItem}
keyExtractor={keyExtractor}
estimatedItemSize={385}
ItemSeparatorComponent={Separator}
scrollEventThrottle={10}
contentContainerStyle={{ paddingTop: headerHeight }}
onScroll={onScroll}
/>