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

2024-03-27

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

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


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

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
  • forRevealFromBottomAndroid
import { CardStyleInterpolators } from '@react-navigation/stack';

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React-native 中 Stack.Screens 之间的 Stack.Navigator 淡入淡出过渡? 的相关文章

随机推荐