How to make draggable remember it's location in React Native

前端 未结 3 731
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 09:40

I\'m learning about the animation and panresponder apis in react native. Following Animated Drag and Drop with React Native and the source

I want to extend the examp

3条回答
  •  春和景丽
    2021-01-05 10:39

    Actually the only thing you need to change is the onPanResponderRelease function. What you are currently doing is applying the delta of the gesture movement to the initial position of the component. What you want to do is applying it to the position at the end of the last gesture. So you somehow need to save the offset. AnimatedValueXY.setOffset to the rescue!

    onPanResponderRelease : (e, gesture) => {
        if(this.isDropZone(gesture)){
            this.setState({
                showDraggable : false
            });
        }else{
            this.state.pan.setOffset({
                x: this.state.pan.x._offset + e.dx,
                y: this.state.pan.y._offset + e.dy,
            })
            this.state.pan.setValue({x: 0, y: 0})
    }
    

    I'm using the internal _offset variable here because I could find a way to access it via the API. You can off course also just keep track of the offset manually. I didn't test this code yet, but you get the idea.

提交回复
热议问题