我想在我的新React Native应用程序中使用React Navigation,但是我找不到任何示例显示如何在其中创建自定义视图转换。默认的过渡效果很好,但我希望能够在某些地方对它们进行自定义,而文档在这个主题上并没有提供很大的帮助。
有人已经尝试过吗?有没有地方可以看到一个工作的示例?
先谢谢了。
我想在我的新React Native应用程序中使用React Navigation,但是我找不到任何示例显示如何在其中创建自定义视图转换。默认的过渡效果很好,但我希望能够在某些地方对它们进行自定义,而文档在这个主题上并没有提供很大的帮助。
有人已经尝试过吗?有没有地方可以看到一个工作的示例?
先谢谢了。
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
});
在您的项目中使用应用程序导航器
class App extends Component {
return (
<View>
<AppNavigator />
</View>
)
}
在eq中注册你的应用程序。index.ios.js
import { AppRegistry } from 'react-native';
AppRegistry.registerComponent('MyApp', () => App);
关于如何为每个场景设置过渡效果,这是我的做法。
当您使用react-navigation
中的NavigationActions
进行导航时,您可以传递一些属性。在我的情况下,它看起来像这样:
this.props.navigate({
routeName: 'SceneTwo',
params: {
transition: 'myCustomTransition'
}
})
然后在配置器内,您可以像这样在这些转换之间切换
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];
}
}
};