【问题标题】:How to change default navigation animation in react native如何在 React Native 中更改默认导航动画
【发布时间】:2019-09-25 19:04:52
【问题描述】:

有什么方法可以改变 react native 中的默认导航动画。在IOS中它从右向左滑动。安卓自下而上。我们可以为这两个平台制作这个概念吗? 非常感谢

【问题讨论】:

    标签: javascript react-native hybrid-mobile-app mobile-development


    【解决方案1】:

    您可以为屏幕设置不同的模式,其中之一是modal

    const MainStackNavigator = createStackNavigator(
      {
        SplashScreen: { screen: SplashScreen },
        OnBoardingScreen: { screen: OnBoarding },
        LoginScreen: { screen: LoginContainer },
      },
      {
        navigationOptions: {
          header: null
        },
        mode: "modal",
        cardStyle: { backgroundColor: "transparent" },
       transitionConfig: TransitionConfiguration,
    
      }
    );
    

    你也可以使用transitionConfig,它返回一个带有两个参数transitionSpecscreenInterpolator的对象。您可以在 transitionSpec 中配置动画计时属性,如持续时间和缓动,在 screenInterpolator 中配置布局转换。 这是一个例子:

        const TransitionConfiguration = () => {
      return {
        transitionSpec: {
          duration: 750,
          easing: Easing.out(Easing.poly(4)),
          timing: Animated.timing,
          useNativeDriver: true,
        },
        screenInterpolator: (sceneProps) => {
          const { layout, position, scene } = sceneProps;
          const width = layout.initWidth;
          const { index, route } = scene
          const params = route.params || {}; // <- That's new
          const transition = params.transition || 'default'; // <- That's new
          return {
            collapseExpand: CollapseExpand(index, position),
            default: SlideFromRight(index, position, width),
          }[transition];
        },
      }
    }
    

    默认情况下,新屏幕从右侧滑动。

    let SlideFromRight = (index, position, width) => {
      const inputRange = [index - 1, index, index + 1];
      const translateX = position.interpolate({
        inputRange: [index - 1, index, index + 1],
        outputRange: [width, 0, 0]
      })
      const slideFromRight = { transform: [{ translateX }] }
      return slideFromRight
    };
    

    transitionSpec 没有什么特别之处。事实上,它看起来很像一个标准的 React Native Animated 示例。我们设置了过渡的持续时间和缓动配置文件,将其配置为基于时间的动画而不是弹簧,并使用本机驱动程序来提高性能。

    screenInterpolator 是魔法发生的地方。 screenInterpolator 是一个 React Navigation 调用的函数,它带有一个我们称之为 sceneProps 的参数。 screenInterpolator(sceneProps) 为堆栈中的每个屏幕调用,返回值用于配置其转换。

    如果 navigation(scene) 有一个参数 { transition : 'collapseExpand' } 屏幕插值器采取的 CollapseExpand 动画。

    let CollapseExpand = (index, position) => {
      const inputRange = [index - 1, index, index + 1];
      const opacity = position.interpolate({
        inputRange,
        outputRange: [0, 1, 1],
      });
    
      const scaleY = position.interpolate({
        inputRange,
        outputRange: ([0, 1, 1]),
      });
    
      return {
        opacity,
        transform: [
          { scaleY }
        ]
      };
    };
    

    【讨论】:

    • 我已经更新了评论。请看看这是否有帮助。
    • 我明白了,我的项目中有 100 多个屏幕,看来我需要为每个屏幕添加自定义代码。而且我认为做这种事情对我的原因没有帮助。如果它的屏幕项目数量较少,这将适合。你对我的案子有什么建议吗?
    • 对于您的情况,可以有一种解决方法:您可以从 transitionConfig 的 sceneProps 中获取 routeName,并且您可以仅对这些路由有条件地应用配置。例如:transitionConfig: sceneProps => ({ transitionSpec: sceneProps.scene.route.routeName === 'Screen2' ? screen2Config : {}, screenInterpolator: ... })
    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多