Stack.Navigator 在 React-native 中的 Stack.Screens 之间淡入淡出过渡?

use*_*734 2 react-native react-navigation react-navigation-stack

如何在 React-native 中为 Stacked Screes 添加过渡效果?

<NavigationContainer>
    <Stack.Navigator
      screenOptions={{
        headerShown: false,
      }}
    >
      <Stack.Screen name="Home" component={HomeScreen} />
      <Stack.Screen name="Stocks" component={StocksScreen} />
    </Stack.Navigator>
</NavigationContainer>
Run Code Online (Sandbox Code Playgroud)

是否有实现淡入/淡出效果的默认方法?

Vad*_*sky 11

最简单的实现淡入淡出效果的方法:

const forFade = ({ current }) => ({
  cardStyle: {
    opacity: current.progress,
  },
});
Run Code Online (Sandbox Code Playgroud)

如果要为整个导航器应用淡入淡出效果:

<Stack.Navigator
   screenOptions={{
      headerShown: false,
      cardStyleInterpolator: forFade,
   }}>
   <Stack.Screen name="Home" component={HomeScreen} />
   <Stack.Screen name="Stocks" component={StocksScreen} />
</Stack.Navigator>

Run Code Online (Sandbox Code Playgroud)

您也可以cardStyleInterpolator通过设置选项申请单屏:

<Stack.Screen 
  name="Home" 
  component={HomeScreen} 
  options={{ cardStyleInterpolator: forFade }}/>
Run Code Online (Sandbox Code Playgroud)

您可以自定义forFade函数以实现其他效果,也可以使用一些预制的插值器,例如:

  • 对于水平IOS
  • 垂直IOS
  • forModalPresentationIOS
  • forFadeFromBottomAndroid
  • forRevealFromBottomAndroid
import { CardStyleInterpolators } from '@react-navigation/stack';

<Stack.Screen
  name="Profile"
  component={Profile}
  options={{
    cardStyleInterpolator: CardStyleInterpolators.forFadeFromBottomAndroid,
  }}
/>;
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:https : //reactnavigation.org/docs/stack-navigator/#animations


J.C*_*ras 5

对于 React Navigation 6.xx,您可以使用动画选项:

<Stack.Screen
        name="Profile"
        component={Profile}
        options={{ animation: 'fade' }}
/>
Run Code Online (Sandbox Code Playgroud)

支持的值:

  • “default”:使用平台默认动画
  • “fade”:淡入或淡出屏幕
  • “flip”:翻转屏幕,需要演示:“modal”(仅限 iOS)
  • “simple_push”:使用平台默认动画,但没有阴影和本机标题过渡(仅限 iOS)
  • “slide_from_bottom”:从底部滑入新屏幕
  • “slide_from_right”:从右侧滑入新屏幕(仅限 Android,在 iOS 上使用默认动画)
  • “slide_from_left”:从左侧滑入新屏幕(仅限 Android,在 iOS 上使用默认动画)
  • “none”:不为屏幕设置动画