如果您使用react-navigation,则可以使用transitionConfig 或StackNavigator 来自定义屏幕转换。原始来源是here。但是它不知道上一屏,只能根据当前屏的信息进行自定义。
转换到 B 的示例是左 => 右,而到 C 的转换是顶部=>底部:
const myNavigator = StackNavigator(
{
A: { screen: A },
B: { screen: B },
C: { screen: C }
},
transitionConfig: () => ({
screenInterpolator: screenProps => {
const { layout, position, scene } = sceneProps
const { index, route: { routeName } } = scene
const width = layout.initWidth
const height = layout.initHeight
let translateX = 0
let translateY = 0
const opacity = position.interpolate({
inputRange: [index - 1, index - 0.99, index],
outputRange: [0, 1, 1],
})
switch (routeName) {
case 'B':
translateX = position.interpolate({
inputRange: [index - 1, index, index + 1],
outputRange: [-width, 0, 0],
})
break;
case 'C':
default:
translateY = position.interpolate({
inputRange: [index - 1, index, index + 1],
outputRange: [-height, 0, 0],
})
}
return {
opacity, transform: [
{ translateX },
{ translateY }
]
}
}
})
)