React Native - React Navigation过渡

kar*_*ski 16 react-native react-navigation

我想在我的新反应本机应用程序中使用React Navigation,但我找不到任何示例显示如何在那里创建自定义视图转换.默认转换工作正常,但我希望能够在很少的地方自定义它们,并且文档在这个主题中没有帮助.有人试过吗?在哪里我能看到一个有效的例子?提前致谢.

Tim*_*vec 70

你可以在这个链接上找到这篇文章的详细版本

我希望这对于如何创建自定义转换具有足够的清晰度.

创建场景或两个以进行导航

class SceneOne extends Component {
    render() {
        return (
            <View>
                <Text>{'Scene One'}</Text>
            </View>
        )
    }
}
class SceneTwo extends Component {
    render() {
        return (
            <View>
                <Text>{'Scene Two'}</Text>
            </View>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

声明您的应用场景

let AppScenes = {
    SceneOne: {
        screen: SceneOne
    },
    SceneTwo: {
        screen: SceneTwo
    },
}
Run Code Online (Sandbox Code Playgroud)

声明自定义转换

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}
        ]
    };
};
Run Code Online (Sandbox Code Playgroud)

声明自定义转换配置器

let TransitionConfiguration = () => {
    return {
        // Define scene interpolation, eq. custom transition
        screenInterpolator: (sceneProps) => {

            const {position, scene} = sceneProps;
            const {index} = scene;

            return MyTransition(index, position);
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

使用Stack Navigator创建应用程序导航器

const AppNavigator = StackNavigator(AppScenes, {
    transitionConfig: TransitionConfiguration
});
Run Code Online (Sandbox Code Playgroud)

在项目中使用App Navigator

class App extends Component {
    return (
        <View>
            <AppNavigator />
        </View>
    )
}
Run Code Online (Sandbox Code Playgroud)

在eq中注册您的应用程序. index.ios.js

import { AppRegistry } from 'react-native';
AppRegistry.registerComponent('MyApp', () => App);
Run Code Online (Sandbox Code Playgroud)

更新#1

至于如何设置每个场景的转换的问题,我就是这样做的.

当您使用NavigationActionsfrom 导航时react-navigation,您可以通过一些道具.就我而言,它看起来像这样

this.props.navigate({
    routeName: 'SceneTwo',
    params: {
        transition: 'myCustomTransition'
    }
})
Run Code Online (Sandbox Code Playgroud)

然后在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];
        }
    }
};
Run Code Online (Sandbox Code Playgroud)