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

问题描述 投票:0回答:2

如何在 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>

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

react-native react-navigation react-navigation-stack
2个回答
30
投票

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

const forFade = ({ current }) => ({
  cardStyle: {
    opacity: current.progress,
  },
});

如果您想对整个导航器应用淡入淡出效果:

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

您还可以通过设置选项将

cardStyleInterpolator
应用于单屏:

<Stack.Screen 
  name="Home" 
  component={HomeScreen} 
  options={{ cardStyleInterpolator: forFade }}/>

您可以自定义

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

  • 适用于水平IOS
  • 垂直IOS
  • 用于ModalPresentationIOS
  • forFadeFromBottomAndroid
  • 用于RevealFromBottomAndroid
import { CardStyleInterpolators } from '@react-navigation/stack';

<Stack.Screen
  name="Profile"
  component={Profile}
  options={{
    cardStyleInterpolator: CardStyleInterpolators.forFadeFromBottomAndroid,
  }}
/>;

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


5
投票

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

<Stack.Screen
        name="Profile"
        component={Profile}
        options={{ animation: 'fade' }}
/>

支持的值:

  • “default”:使用平台默认动画
  • “淡入淡出”:淡入或淡出屏幕
  • “flip”:翻转屏幕,需要演示:“modal”(仅限 iOS)
  • “simple_push”:使用平台默认动画,但没有阴影和原生标题过渡(仅限 iOS)
  • “slide_from_bottom”:从底部滑入新屏幕
  • “slide_from_right”:从右侧滑入新屏幕(仅限 Android,iOS 上使用默认动画)
  • “slide_from_left”:从左侧滑入新屏幕(仅限 Android,iOS 上使用默认动画)
  • “none”:不要为屏幕设置动画
© www.soinside.com 2019 - 2024. All rights reserved.