假设我有两个视图,A和B.我希望能够在touch 视图A时触发"dragAndDropStart"事件,然后启用从A到B的拖放...始终向用户显示反馈(即显示视图a和用户手指之间的线条).在drop(释放拖动手势)时,我想触发另一个"dragAndDropEnd"事件,这次是在视图B上.

在此处输入图像描述

touchStart和touchEnd处理程序过于有限,因为它们似乎不允许手势从一个视图切换到另一个视图.它们似乎也不会启用中间的"拖动"状态.

关于使用手势处理程序的React native docs有点晦涩,我还没有看到任何示例演示它们的用法.

有什么 idea 吗?

推荐答案

export default class Viewport extends Component{
    constructor(props){
        super(props);

        this.state = {
            showDraggable   : true,
            dropZoneValues  : null,
            pan             : new Animated.ValueXY()
        };

        this.panResponder = PanResponder.create({
            onStartShouldSetPanResponder    : () => true,
            onPanResponderMove              : Animated.event([null,{
                dx  : this.state.pan.x,
                dy  : this.state.pan.y
            }]),
            onPanResponderRelease           : (e, gesture) => {
                if(this.isDropZone(gesture)){
                    this.setState({
                        showDraggable : false
                    });
                }else{
                    Animated.spring(
                        this.state.pan,
                        {toValue:{x:0,y:0}}
                    ).start();
                }
            }
        });
    }

    isDropZone(gesture){
        var dz = this.state.dropZoneValues;
        return gesture.moveY > dz.y && gesture.moveY < dz.y + dz.height;
    }

    setDropZoneValues(event){
        this.setState({
            dropZoneValues : event.nativeEvent.layout
        });
    }

    render(){
        return (
            <View style={styles.mainContainer}>
                <View 
                    onLayout={this.setDropZoneValues.bind(this)}
                    style={styles.dropZone}>
                    <Text style={styles.text}>Drop me here!</Text>
                </View>

                {this.renderDraggable()}
            </View>
        );
    }

    renderDraggable(){
        if(this.state.showDraggable){
            return (
                <View style={styles.draggableContainer}>
                    <Animated.View 
                        {...this.panResponder.panHandlers}
                        style={[this.state.pan.getLayout(), styles.circle]}>
                        <Text style={styles.text}>Drag me!</Text>
                    </Animated.View>
                </View>
            );
        }
    }
}

来源http://moduscreate.com/animated_drag_and_drop_with_react_native/

https://github.com/crysfel/DragAndDrop

React-native相关问答推荐

构建 iOS Expo 应用时出现 React-Native xcodebuild 错误 65

React Native 应用程序没有在之前的工作代码中执行任何操作就无法运行

IntelliSense 在第一项后无法在 React Native 样式表中工作

滚动到具有可变元素大小的 FlatList 中的某些索引的有效方法

最近 react-native 版本中的Unable to resolve moduledebuggerWorker 错误

react-native app.js index.js

在 react-native 元素中更改 android 和 ios 的入口文件路径

试图注册两个同名的视图 RNGestureHandlerButton

如何在 array.map 中的每个元素之后添加逗号,除了 React JSX 中的最后一个元素

在 React Native 中无法滚动到 ScrollView 的底部

我们如何将 Firebase Analytics 与基于 expo 的 react-native 应用程序一起使用

如何在我的 React Native Android 应用程序中显示当前的 CodePush 版本标签?

大型列表的 React-Native FlatList 性能问题

在 react-native-maps 中渲染多个标记

任务在 react-native 中因请求而成为orphaned - 这是什么意思?

React本机IOS,当TextInput获得焦点时,按钮按下不注册

React Context:TypeError:render is not a function

在 react-native 中使用捏拉zoom 的可滚动图像

React Native:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败

如何在 React-Native 中停止touch 事件传播