react-native-gesture-handler 手势的使用

要在React Native项目中使用react-native-gesture-handler,可以按照以下步骤进行设置:

1、首先,在你的React Native项目中安装react-native-gesture-handler。可以使用npm或者yarn命令来安装:

javascript 复制代码
npm install react-native-gesture-handler

或者

复制代码
yarn add react-native-gesture-handler

2、安装完成后,需要链接react-native-gesture-handler到你的原生代码。运行以下命令:

复制代码
npx react-native link react-native-gesture-handler

3、接下来,在你的项目中导入GestureHandler库。在你的入口文件(通常是App.js或index.js)中添加以下内容:

复制代码
import 'react-native-gesture-handler';

4、然后,你需要在你的原生代码中进行一些额外的配置。具体的配置取决于你使用的平台。

  • 对于Android平台,在android/app/src/main/java/com/<your-app>/MainApplication.java文件中添加以下导入语句:
java 复制代码
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

然后,修改getMainComponentName方法如下:

java 复制代码
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
  return new ReactActivityDelegate(this, getMainComponentName()) {
  @Override
  protected ReactRootView createRootView() {
    return new RNGestureHandlerEnabledRootView(MainApplication.this);
  }
  };
}
  • 对于iOS平台,如果您使用的是CocoaPods进行包管理,可以通过在项目的Podfile中添加以下代码来简化这个过程:
objectivec 复制代码
pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler'

然后,执行 pod install。

6、接下来,你可以在你的React Native组件中使用react-native-gesture-handler提供的手势组件了。例如,你可以使用TapGestureHandlerPanGestureHandler来处理点击和拖动手势。

javascript 复制代码
import { useState } from 'react'
import { Text, View } from 'react-native'
import { Gesture, GestureDetector, GestureHandlerRootView } from "react-native-gesture-handler";

export default function registerScreens() {
    const [a, setA] = useState("1"),
    [b, setB] = useState("2"),
    [c, setC] = useState("3");

  const gesture = Gesture.Pan()
    .onBegin(() => {
        setA("A");
    })
    .onUpdate(({ translationX, translationY }) => {
        setB("B");
    })
    .onEnd(({ velocityX, velocityY }) => {
        setC("C");
    });

  return (
    <GestureHandlerRootView>
        <GestureDetector gesture={gesture}>
            <View>
                <Text>{ a }</Text>
                <Text>{ b }</Text>
                <Text>{ c }</Text>
            </View>
        </GestureDetector>
    </GestureHandlerRootView>
    )
}

以上是使用react-native-gesture-handler的基本步骤。你可以根据文档进一步了解各种手势组件和属性的使用方式。

相关推荐
Java追光着1 天前
React Native 自建 JS Bundle OTA 更新系统:从零到一的完整实现与踩坑记录
javascript·react native·react.js
努力往上爬de蜗牛1 天前
react native 运行问题和调试 --持续更新
javascript·react native·react.js
一头小鹿1 天前
【React Native】如何在开发中使用Appwrite
react native
GISer_Jing2 天前
跨平台Hybrid App开发实战指南
android·flutter·react native
努力学前端Hang2 天前
移动端跨平台开发深度解析:UniApp、Taro、Flutter 与 React Native 对比
前端·javascript·react native·react.js
浪遏3 天前
好久不见 ,甚是想念 | vibe coding 一个react native 全栈项目| 小账兜
react native·全栈·vibecoding
GISer_Jing4 天前
跨端框架对决:React Native vs Flutter深度对比
flutter·react native·react.js
探索宇宙真理.5 天前
React Native Community CLI命令执行 | CVE-2025-11953 复现&研究
javascript·经验分享·react native·react.js·安全漏洞
Cxiaomu6 天前
React Native App 自动检测版本更新完整实现指南
javascript·react native·react.js
光影少年7 天前
React Native第六章
javascript·react native·react.js