CustomTransitioner.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { Component, PropTypes } from 'react';
  2. import {
  3. Animated,
  4. Easing,
  5. Image,
  6. Platform,
  7. StatusBar,
  8. StyleSheet,
  9. View,
  10. } from 'react-native';
  11. import {
  12. Transitioner,
  13. SafeAreaView,
  14. StackRouter,
  15. createNavigationContainer,
  16. createNavigator,
  17. } from 'react-navigation';
  18. import SampleText from './SampleText';
  19. import { Button } from './commonComponents/ButtonWithMargin';
  20. const MyNavScreen = ({ navigation, banner }) => (
  21. <SafeAreaView forceInset={{ top: 'always' }}>
  22. <SampleText>{banner}</SampleText>
  23. {navigation.state &&
  24. navigation.state.routeName !== 'Settings' && (
  25. <Button
  26. onPress={() => navigation.navigate('Settings')}
  27. title="Go to a settings screen"
  28. />
  29. )}
  30. <Button onPress={() => navigation.goBack(null)} title="Go back" />
  31. <StatusBar barStyle="default" />
  32. </SafeAreaView>
  33. );
  34. const MyHomeScreen = ({ navigation }) => (
  35. <MyNavScreen banner="Home Screen" navigation={navigation} />
  36. );
  37. const MySettingsScreen = ({ navigation }) => (
  38. <MyNavScreen banner="Settings Screen" navigation={navigation} />
  39. );
  40. class CustomNavigationView extends Component {
  41. render() {
  42. const { navigation, router, descriptors } = this.props;
  43. return (
  44. <Transitioner
  45. configureTransition={this._configureTransition}
  46. descriptors={descriptors}
  47. navigation={navigation}
  48. render={this._render}
  49. />
  50. );
  51. }
  52. _configureTransition(transitionProps, prevTransitionProps) {
  53. return {
  54. duration: 200,
  55. easing: Easing.out(Easing.ease),
  56. };
  57. }
  58. _render = (transitionProps, prevTransitionProps) => {
  59. const scenes = transitionProps.scenes.map(scene =>
  60. this._renderScene(transitionProps, scene)
  61. );
  62. return <View style={{ flex: 1 }}>{scenes}</View>;
  63. };
  64. _renderScene = (transitionProps, scene) => {
  65. const { navigation, router } = this.props;
  66. const { routes } = navigation.state;
  67. const { position } = transitionProps;
  68. const { index } = scene;
  69. const animatedValue = position.interpolate({
  70. inputRange: [index - 1, index, index + 1],
  71. outputRange: [0, 1, 0],
  72. });
  73. const animation = {
  74. opacity: animatedValue,
  75. transform: [{ scale: animatedValue }],
  76. };
  77. const Scene = scene.descriptor.getComponent();
  78. return (
  79. <Animated.View key={index} style={[styles.view, animation]}>
  80. <Scene navigation={scene.descriptor.navigation} />
  81. </Animated.View>
  82. );
  83. };
  84. }
  85. const CustomRouter = StackRouter({
  86. Home: { screen: MyHomeScreen },
  87. Settings: { screen: MySettingsScreen },
  88. });
  89. const CustomTransitioner = createNavigationContainer(
  90. createNavigator(CustomNavigationView, CustomRouter, {})
  91. );
  92. export default CustomTransitioner;
  93. const styles = StyleSheet.create({
  94. view: {
  95. position: 'absolute',
  96. left: 0,
  97. right: 0,
  98. top: 0,
  99. bottom: 0,
  100. },
  101. });