react-native – 如何使draggable记住它在React Native中的位置
我正在学习动画和panresponder apis in react native.继
Animated Drag and Drop with React Native和
source之后
我想扩展示例,以便您可以拖动圆圈而无需重置它.目前,代码将圆圈设置回屏幕中心,并依赖于panresponders dX / dY值. 我最好的猜测是我必须在onPanResponderMove中改变一些东西 onPanResponderMove: Animated.event([null,{ dx: this.state.pan.x,dy: this.state.pan.y,}]), 我需要在源代码中进行哪些更改,因此如果我注释掉onPanResponderRelease逻辑,则圆圈会在屏幕上正确拖动? getTranslateTransform()?
onPanResponderRelease中的逻辑使得如果在释放时拖动区不在dropzone中,它将重置回0,0(
these are the lines causing it).
尽管如此,删除该逻辑并不是你应该做的.您应该设置偏移量并在onPanResponderRelease中重置this.state.pan的值.为此,您需要跟踪该值. 在componentDidMount中,添加以下内容: this.currentPanValue = {x: 0,y: 0}; this.panListener = this.state.pan.addListener((value) => this.currentPanValue = value); 然后,在componentWillUnmount中: this.state.pan.removeListener(this.panListener); 现在您已拥有当前值,只需将其添加到PanResponder: onPanResponderRelease: (e,gestureState) => { this.state.pan.setOffset({x: this.currentPanValue.x,y: this.currentPanValue.y}); this.state.pan.setValue({x: 0,y: 0}); }, 它似乎比实际上更复杂.基本上你只是设置从0/0的偏移量,然后将值设置为0/0,这样当你在之前释放它之后再次开始移动它时,它不会跳回到0/0,然后再跳回你的位置.手指是.它还确保您拥有当前位置……无论如何您可能需要它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- SBJson的使用
- ruby-on-rails – 跳过模型创建脚手架
- SQLite指南(2) -- 帮助及编译SQLite
- sqlite学习,如何创建数据库与增删改查
- ruby-on-rails – YAML :: load引发未定义的类/模块错误
- reactjs – 不推荐使用主React包访??问PropTypes
- How is the space pressure managed in the Flash Recovery
- ruby – 如何访问webrick提供的.rhtml页面的html请求参数?
- ajax实现加载页面、删除、查看详细信息 bootstrap美化页面!
- cocos2d-js 3.x 新手笔记