ios - 如何在 React Native 中创建拖放操作?

标签 ios drag-and-drop gesture react-native

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

enter image description here

touchStart 和 touchEnd 处理程序非常有限,因为它们似乎不允许将手势从一个 View 切换到另一个 View 。它们似乎也没有启用中间的“拖动”状态。

关于使用手势处理程序的 React native 文档有点含糊不清,我还没有看到任何示例来演示它们的用法。

有什么想法吗?

最佳答案

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>
            );
        }
    }
}

来源https://moduscreate.com/blog/drag-and-drop-react-native/

https://github.com/crysfel/DragAndDrop

关于ios - 如何在 React Native 中创建拖放操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30153285/

相关文章:

iOS 测试 : Is there a way to skip tests?

jquery - 使用 jQuery 拖放时保留元素的副本

具有精美拖放功能的 Java 应用程序

windows - 在 Win7 机器上拖放到我的应用程序

android - 如何在android中以编程方式创建手势?

ios - 分镜设计——如何展现标准的发际线?

ios - RxSwift 订阅数据模型属性更改的正确方法

ios - 无法保存 AFNetworking responseObject

java - Android 中双击恢复 Canvas

c# - 处理触摸和手势事件