我想在新的react native应用程序中使用React Navigation,但找不到任何示例显示如何在其中创建自定义 View 过渡。默认过渡效果很好,但我希望能够在几个地方自定义它们,而文档在此主题中并不是很有帮助。
有人尝试过吗?我能在任何地方看到有效的示例吗?
提前致谢。
最佳答案
您可以在this link上找到此帖子的详细版本
我希望通过逐步了解如何创建自定义过渡已经足够清楚。
创建一个或两个场景进行导航
class SceneOne extends Component {
render() {
return (
<View>
<Text>{'Scene One'}</Text>
</View>
)
}
}
class SceneTwo extends Component {
render() {
return (
<View>
<Text>{'Scene Two'}</Text>
</View>
)
}
}
声明您的应用场景
let AppScenes = {
SceneOne: {
screen: SceneOne
},
SceneTwo: {
screen: SceneTwo
},
}
声明自定义过渡
let MyTransition = (index, position) => {
const inputRange = [index - 1, index, index + 1];
const opacity = position.interpolate({
inputRange,
outputRange: [.8, 1, 1],
});
const scaleY = position.interpolate({
inputRange,
outputRange: ([0.8, 1, 1]),
});
return {
opacity,
transform: [
{scaleY}
]
};
};
声明自定义过渡配置器
let TransitionConfiguration = () => {
return {
// Define scene interpolation, eq. custom transition
screenInterpolator: (sceneProps) => {
const {position, scene} = sceneProps;
const {index} = scene;
return MyTransition(index, position);
}
}
};
使用Stack Navigator创建应用程序导航器
const AppNavigator = StackNavigator(AppScenes, {
transitionConfig: TransitionConfiguration
});
在项目中使用App Navigator
class App extends Component {
return (
<View>
<AppNavigator />
</View>
)
}
在eq中注册您的应用。
index.ios.js
import { AppRegistry } from 'react-native';
AppRegistry.registerComponent('MyApp', () => App);
更新#1
至于如何设置每个场景的过渡的问题,这就是我的做法。
使用
NavigationActions
中的react-navigation
导航时,可以通过一些 Prop 。就我而言,它看起来像这样this.props.navigate({
routeName: 'SceneTwo',
params: {
transition: 'myCustomTransition'
}
})
然后在Configurator中,您可以像这样在这些转换之间切换
let TransitionConfiguration = () => {
return {
// Define scene interpolation, eq. custom transition
screenInterpolator: (sceneProps) => {
const {position, scene} = sceneProps;
const {index, route} = scene
const params = route.params || {}; // <- That's new
const transition = params.transition || 'default'; // <- That's new
return {
myCustomTransition: MyCustomTransition(index, position),
default: MyTransition(index, position),
}[transition];
}
}
};