当触摸拖动而不是按下时,如何取消触摸式响应器



我正在尝试实现我自己的反应型swiper组件,但是当animated.view中添加触摸可触摸时,刀刀会立即停止工作。我认为可接触的人将成为响应者,我自己的panresponder将行不通。(您可以看到按压的不透明度)

问题是我希望触摸可碰到,但是如果屏幕上的触摸不是一个点击,而是拖动,那么我希望swiper工作。(此行为在使用scrollview与接触物时正在起作用)

在这里:开始滚动时,使触摸可触摸不是突出显示元素[REECT NATICE]被说:"滚动手势应该取消触摸式触摸响应者",但是如何完成?

出于测试目的,我试图捕获动画的响应者。请参见以下内容:

onStartShouldSetResponderCapture: () => true,

,但似乎没有效果(我期望通过这一点,刷卡会起作用,但可以接触)。

这是示例代码(如果更改可触摸的权限,以查看刷新正在工作):

import React, { Component } from 'react';
import { View, Animated, PanResponder, Dimensions, TouchableOpacity, Text } from 'react-native';
class App extends Component {
  componentWillMount() {
    const position = new Animated.ValueXY();
    const panResponder = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onPanResponderMove: (event, gesture) => {
        position.setValue({ x: gesture.dx, y: 0 })
      },
      onPanResponderRelease: (event, gesture) => {
          Animated.spring(this.state.position, {
                toValue: { x: 0, y: 0 }
              }).start();
      }
    });
    this.state = { panResponder, position };
  }
  render() {
    return (
          <Animated.View style={[this.state.position.getLayout(), styles.viewStyle]} {...this.state.panResponder.panHandlers}>
              <TouchableOpacity style={styles.touchableStyle}>
                     <Text>Swipe not working</Text>
              </TouchableOpacity>
          </Animated.View>
    );
  }
}
const styles = {
  viewStyle: {
    position: 'absolute',
    top: 0,
    bottom: 0,
    width: Dimensions.get('window').width
  },
  touchableStyle: {
    backgroundColor: '#ddd',
    height: 100,
    borderWidth: 5,
    borderColor: '#000',
    justifyContent: 'center',
    alignItems: 'center'
  }
};
export default App;

如果您有解决方案,请帮助我。我已经试图让它工作了一段时间。

它设法解决了问题:

onStartShouldSetPanResponder: () => true

必须替换为:

onMoveShouldSetPanResponder: () => true

不透明度Higlighting仍然存在同样的问题,但这并不是那么大的问题。开始滚动时,使触摸可触摸不是突出显示元素[react Native]

最新更新