使用react-native中的Animation隐藏和显示createBottomTabNavigator标签栏

时间:2019-02-08 09:10:45

标签: javascript android ios react-native

我正在使用createBottomTabNavigator作为标签栏。

我可以通过将tabBarVisibletrue设置为false来隐藏和显示标签栏。

我遇到的问题是,我希望它随动画一起隐藏。

任何链接都会有帮助。

2 个答案:

答案 0 :(得分:2)

您可能想使用新的Animated.Value(0) 并更改选项卡的底值。 https://github.com/react-navigation/react-navigation/issues/888这有一个解决方案。

答案 1 :(得分:0)

您可以创建自定义tabBarComponent,然后使用所需的动画对其进行隐藏/显示。我在tabbar

中抓住了componentWillReceiveProps的道具

我用react-native-animatable做动画。

  componentWillReceiveProps(props) {
    const oldState = this.props.navigation.state;
    const oldRoute = oldState.routes[oldState.index].routes[0];
    const oldParams = oldRoute.params;
    const wasVisible = !oldParams || oldParams.visible;

    const newState = props.navigation.state;
    const newRoute = newState.routes[newState.index].routes[0];
    const newParams = newRoute.params;
    const isVisible = !newParams || newParams.visible;

    if (wasVisible && !isVisible) {
      this.view.slideOutDown(500);
      this.setState({
        hidden: true,
      });
    } else if (isVisible && !wasVisible) {
     this.view.slideInUp(500).then(() => {
      this.setState({
        hidden: false,
      });
    });
    } 
  }

  render() {
    return (
      <Animatable.View
        ref={ref => { this.view = ref; }}
        style={[styles.container, {
          position: this.state.hidden ? 'absolute' : 'relative',
        }]}
        useNativeDriver
      >
        <BottomTabBar
          {...this.props}
        />
      </Animatable.View>
    );
  }