Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native React本机PanResponder偏移量不工作_React Native_Panresponder - Fatal编程技术网

React native React本机PanResponder偏移量不工作

React native React本机PanResponder偏移量不工作,react-native,panresponder,React Native,Panresponder,当用户第一次释放时,我试图保持平移Y偏移量-我已经研究了如何使用以下方法实现这一点,但偏移量被忽略: constructor(props) { super(props); this._pan = new Animated.ValueXY(); this._pan.addListener(value => { this._value = value; const {height: windowHeight} = Dimensions.get('window'); this.p

当用户第一次释放时,我试图保持平移Y偏移量-我已经研究了如何使用以下方法实现这一点,但偏移量被忽略:

constructor(props) {
super(props);

this._pan = new Animated.ValueXY();

this._pan.addListener(value => {
  this._value = value;
  const {height: windowHeight} = Dimensions.get('window');
  this.props.onZoomProgress(
    Math.min(Math.max((value.y * -1) / windowHeight, 0), 0.5),
  );
});

this._panResponder = PanResponder.create({
  onMoveShouldSetResponderCapture: () => true,
  onMoveShouldSetPanResponderCapture: () => true,

  onPanResponderGrant: (e, gestureState) => {
    this._pan.setOffset({x: 0, y: gestureState.dy});
    this._pan.setValue({x: 0, y: 0});
  },

  onPanResponderMove: (e, gestureState) => {
    this._pan.setValue({y: gestureState.dy, x: 0});
    console.log(this._pan.y);
  },

  onPanResponderRelease: (e, gestureState) => {
    this._pan.extractOffset();
  },
});
}
这是此功能的一个单独组件,因此我使用This.props.onZoomProgress传递值,以用作主组件中的缩放状态


干杯

我不确定这是否是您的问题,但您直接访问Animated.ValueXY的内部值的部分在我看来是可疑的。通常,如果你想得到一个实际的数字,而不是仅仅让动画值来完成这项工作,你会在其中添加一个监听器

大概是这样的:

this._pan.addListener(value => {
  this._value = value;
  const {height: windowHeight} = Dimensions.get('window');
  this.props.onZoomProgress(Math.min(Math.max((value.y * -1) / windowHeight, 0), 0.5))
});

需要注意的重要一点是,您不能直接获取动画值的值,您可以将对象指定给动画组件并进行神奇的更新,或者设置一个侦听器,并在传递给您时获取值。

我认为您可以从侦听器调用ZoomProgress,使用传入的值。我不太确定您的意思-就像onPanResponderMove之外?当我让侦听器和控制台记录它正确显示的值时,但是当我使用onZoomProgress传递它时,我收到一个错误,说它是我添加的。谢谢,这很好,感觉更干净,但是我的Y偏移仍然被忽略,缩放/平移响应器在第一次执行后重置