检测另一个视图何时被触摸-在react native中使用PanResponder拖动
detect when another view is touched - dragging with PanResponder in react native
我有一个纸牌游戏,用户可以在屏幕上拖动纸牌。
如何检测卡片是否已被拖过其他视图组件?我的可拖动卡代码是这样的:
// A draggable card
// drag drop code example used: https://github.com/brentvatne/react-native-animated-demo-tinder
// panresponder docs: https://facebook.github.io/react-native/docs/panresponder.html
class Card extends React.Component {
componentWillMount() {
this.state = {
pan: new Animated.ValueXY(),
enter: new Animated.Value(1),
}
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderGrant: (e, gestureState) => {
Animated.spring(this.state.enter, {
toValue: .75,
}).start()
this.state.pan.setOffset({x: this.state.pan.x._value,
y: this.state.pan.y._value});
this.state.pan.setValue({x: 0, y: 0});
},
onPanResponderMove: Animated.event([
null, {dx: this.state.pan.x, dy: this.state.pan.y},
]),
onPanResponderRelease: (e, {vx, vy}) => {
// do stuff when card released
}
Animated.spring(this.state.enter, {
toValue: 1,
}).start()
this.state.pan.flattenOffset();
var velocity;
if (vx >= 0) {
velocity = clamp(vx, 3, 5);
} else if (vx < 0) {
velocity = clamp(vx * -1, 3, 5) * -1;
}
Animated.spring(this.state.pan, {
toValue: {x: 0, y: toValue},
friction: 4
}).start()
}
})
}
我不知道这是否是最好的方法,但我只会得到目标视图的onLayout
,以获得x、y、宽度和高度。。。
<View onLayout={({nativeEvent: {layout: {x,y,width,height}}) => { })} />
容器覆盖的坐标将简单地为(x,x+宽度)和(y,y+高度)
onPanResponderGrant
获取可拖动对象的位置:
onPanResponderGrant: (e) => {
this.originX = e.pageX - e.locationX;
this.originY = e.pageY - e.locationY;
}
onPanResponderMove
你可以这样做:
onPanResponderMove: (e, gestureState) => {
const currentX0 = this.originX + gestureState.dx
const currentY0 = this.originY + gestureState.dy
}
现在可拖动的范围包括从currentX0
到currentX0 + width
以及从currentY0
到currentY0 + height
。。。
您可以使用这些值来检查它是否与目标视图(x, x+width)
和(y, y+height)
重叠
相关文章:
- 调整窗口大小时,可拖动的对象会出现在容器外部
- 删除对HTML元素的拖动
- jQuery UI可排序-多连接列表拖动
- 禁用SVG拖动
- 旋转后拖动对象
- JQuery UI可拖动潜水与滚动棒到鼠标
- jsPlumb-拖动克隆而不进行复制
- 当我在节点上拖动鼠标时,我如何防止使用d3.ehavior.zoom().on(“缩放”,重绘)
- 如何使元素在可拖动元素内可单击
- 如何在使用Kinetic拖动同一对象后,在旋转动画中保持先前的偏移
- 在d3中拖动一条线
- 使元素在iframe中可拖动
- 通过拖动可以更改三维模型对象在Three.js场景中的位置
- 如何使所有绘制的(矩形、圆形、直线、多边形)都可拖动?纯JS
- 拖动开始时更改可拖动元素的大小
- 如何正确拖动jqgrid编辑表单
- 交互.js拖动的项目移动到顶部
- 拖动&删除Fullcalendar.io资源-更新视图
- Ember.js可拖动和可拖放的jqueryUI / native Drag and drop mixin
- 检测另一个视图何时被触摸-在react native中使用PanResponder拖动