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

前端 未结 3 742
没有蜡笔的小新
没有蜡笔的小新 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

    Another way would be to track current offset and keep adding it to the pan. So declare this in the constructor currentPanValue : {x: 0, y: 0},

    And edit the onPanResponderRelease to the following :

    onPanResponderRelease           : (e, gesture) => {
                    this.state.currentPanValue.x += this.state.pan.x._value;
                    this.state.currentPanValue.y += this.state.pan.y._value;
    
                    this.state.pan.setOffset({x: this.state.currentPanValue.x, y: this.state.currentPanValue.y});
                    this.state.pan.setValue({x: 0, y: 0});
                }
    

    Incase if you want the final coordinates, you should get it with this.state.currentPanValue

提交回复
热议问题