【问题标题】:React-navigation stack horizontal screen gesture for Android. (react-navigation/stack)适用于 Android 的 React-navigation 堆栈水平屏幕手势。 (反应导航/堆栈)
【发布时间】:2021-06-18 03:31:55
【问题描述】:

在 Android 上,我知道当使用 react-navigation-stack 库和 transitionConfig 属性时,可以将堆栈屏幕动画控制为水平。但是,它似乎不适用于 react-navigation/stack 库,或者说它没有任何支持。有没有人设法使它与react-navigation/stack 一起工作?或者有什么方法可以将这两个库结合起来,因为我发现仅仅为了这个小问题而在另一个库中重写我的代码是一种浪费。任何帮助将不胜感激!

【问题讨论】:

  • Stack Navigator 有 gestureDirection 选项。你试过it吗?
  • 是的,我试过了,但是不行。

标签: react-native expo react-navigation


【解决方案1】:

像这样使用它-

Working Example

Youtube Tutorial for Navigation Gestures

import * as React from 'react';
import { Button, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { Easing } from 'react-native';
import {
  createStackNavigator,
  CardStyleInterpolators,
} from '@react-navigation/stack';

function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button
        title="Go to Profile"
        onPress={() => navigation.navigate('Profile')}
      />
    </View>
  );
}

function ProfileScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button
        title="Go to Notifications"
        onPress={() => navigation.navigate('Notifications')}
      />
      <Button title="Go back" onPress={() => navigation.goBack()} />
    </View>
  );
}

function NotificationsScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button
        title="Go to Settings"
        onPress={() => navigation.navigate('Settings')}
      />
      <Button title="Go back" onPress={() => navigation.goBack()} />
    </View>
  );
}

function SettingsScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button title="Go back" onPress={() => navigation.goBack()} />
    </View>
  );
}

const Stack = createStackNavigator();

const timingConfig = {
  animation: 'timing',
  config: {
    duration: 200,
    easing: Easing.linear,
  },
};

const options = {
  gestureEnabled: true,
  cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
  transitionSpec: {
    open: timingConfig,
    close: timingConfig,
  },
};

function MyStack() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen} options={options} />
      <Stack.Screen
        name="Notifications"
        component={NotificationsScreen}
        options={options}
      />
      <Stack.Screen
        name="Profile"
        component={ProfileScreen}
        options={options}
      />
      <Stack.Screen
        name="Settings"
        component={SettingsScreen}
        options={options}
      />
    </Stack.Navigator>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <MyStack />
    </NavigationContainer>
  );
}

【讨论】:

  • 在尝试了一下之后,我意识到Android上的水平手势效果可以在没有transitionSpec选项的情况下使用,cardStyleInterpolator就足够了。
猜你喜欢
  • 1970-01-01
  • 2017-08-22
  • 2019-07-27
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多