react-native - 如何使用React Native中的Animated API同时隐藏页眉和页脚

标签 react-native collapsable

我只是想知道如何同时隐藏页眉和页脚并具有相同的动画值?

因为我认为我不能同时对多个组件使用相同的动画值。

我的组件

<SafeAreaView style={{ flex: 1 }}>
            <Animated.View
                style={{
                    transform: [{ translateY }],
                    position: "absolute",
                    top: 0,
                    left: 0,
                    zIndex: 100,
                    width: "100%",
                }}
            >
                <MainHeader logo="socialLogo" navigation={navigation} />
            </Animated.View>
            <Animated.FlatList
                ref={ref}
                onEndReachedThreshold={0.5}
                contentContainerStyle={{ paddingTop: HEADER_HEIGHT }}
                bounces={false}
                onScroll={handleScroll}
                nestedScrollEnabled={true}
                ListHeaderComponent={<Stories data={data} app={app} />}
                pinchGestureEnabled={false}
                ListEmptyComponent={<FeedItemLazy />}
                onMomentumScrollEnd={handleSnap}
                scrollEventThrottle={16}
                renderScrollComponent={(props) => <ScrollView {...props} />}
                showsVerticalScrollIndicator={false}
                maxToRenderPerBatch={10}
                onEndReached={() => {
                    props.social.getFeeds.executeNext({ lastID: lastId });
                }}
                refreshControl={
                    <RefreshControl
                        refreshing={isRefreshing}
                        onRefresh={handleRefresh}
                    />
                }
                data={data}
                renderItem={({ item, index }) => (
                    <>
                        <FeedItem
                            data={item}
                            app={app}
                            navigation={navigation}
                            social={social}
                        />
                        <Separator height={2} />
                    </>
                )}
                keyExtractor={(item) => item._id}
            />
            {social.getFeeds.isExecuting && (
                <LottieView
                    source={require("../../../../assets/animation/16337-banana-loader.json")}
                    loop
                    autoPlay
                    style={{ width: 50, height: 50 }}
                />
            )}
            <ModalSelector navigation={navigation} />
        </SafeAreaView>

这是一个 Feed 组件。用户提要列在 Flatlist 中,我的目标是当用户向下滚动时使 MainHeader 和 Bottom 选项卡可折叠。我认为最难的部分是使底部选项卡不可见,因为它们直接来自 react-navigation v5。来自createBottomTabNavigator。我不知道如何将 translateY 值传输到选项卡导航器。

最佳答案

import React from 'react';
import {
  Animated,
  Easing,
  Button,
  View,
  StyleSheet,
  Text,
  TouchableOpacity,
} from 'react-native';

const App = () => {
  const animatedValue = React.useRef(new Animated.Value(0)).current;
  const [hidden, setHidden] = React.useState(false);

  const startAnimation = (toValue) => {
    Animated.timing(animatedValue, {
      toValue: hidden ? 0 : 1,
      duration: 700,
      easing: Easing.linear,
      useNativeDriver: true,
    }).start(() => setHidden(!hidden));
  };

  const translateY = animatedValue.interpolate({
    inputRange: [0, 1],
    outputRange: [0, -50],
    extrapolate: 'clamp',
  });

  return (
    <View style={{ flex: 1 }}>
      <Animated.View
        style={[styles.item, { top: 0, transform: [{ translateY }] }]}
      />
      <TouchableOpacity
        style={styles.button}
        onPress={startAnimation.bind(null, 1 - animatedValue.__value)}>
        <Text>Hide</Text>
      </TouchableOpacity>
      <Animated.View
        style={[
          styles.item,
          {
            bottom: 0,
            transform: [{ translateY: Animated.multiply(translateY, -1) }],
          },
        ]}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  item: {
    width: '100%',
    height: 50,
    position: 'absolute',
    backgroundColor: 'red',
  },
  button: {
    width: '50%',
    height: 50,
    backgroundColor: 'pink',
    justifyContent: 'center',
    alignItems: 'center',
    alignSelf: 'center',
    borderRadius: 25,
    position: 'absolute',
    top: 200,
  },
});

export default App;

直播Demo .

关于react-native - 如何使用React Native中的Animated API同时隐藏页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65237421/

相关文章:

javascript - react 导航隐藏状态栏在标题上方留下空间

android - 在 Android 上更改高度时,react-native TextInput 显示错误

html - CSS关于纯css的折叠菜单

html - Bootstrap 4 : Display two lines followed by a Read More expandable link

css - 如何仅在折叠菜单(又名移动设备)上设置可折叠子菜单

react-native - 在 React Native 中检测热重载

ios - react native : Sample App crashes after upgrading to Mac 10. 12.6

react-native - 在 react-native-router-flux 中以编程方式隐藏导航栏

javascript - 如何通过 knockout 部分(重新)将绑定(bind)应用于后代?

javascript - 页面重新加载后保持折叠和展开状态