简体   繁体   English

同时多个触摸响应器/动作

[英]Multiple touch responders/actions simultaneously

I have 2 pairs of <View> inside <TouchableNativeFeedback> that act as 2 separate counters that trigger when touched, but I'm not able to touch them simultaneously. 我在<TouchableNativeFeedback>中有2对<View> ,它们充当2个单独的计数器,在被触摸时会触发,但是我无法同时触摸它们。

class TouchBox extends Component {
  constructor(props) {
    super(props);
    this.state = {counter: 0}
  }

  render() {
    return (
      <TouchableNativeFeedback style={{flex: 1}} onPress={() => {this.setState({counter: this.state.counter + 1})}}>
        <View style={this.props.style}>
          <Text style={{fontSize: 75}}>{this.state.counter}</Text>
        </View>
      </TouchableNativeFeedback>
    );
  }
}

I tried doing: 我试着做:

<View style={this.props.style} onStartShouldSetResponder={() => {this.setState({counter: this.state.counter + 1}}>
          <Text style={{fontSize: 75}}>{this.state.counter}</Text>
</View>

but it didn't work. 但这没用。

My main component is: 我的主要组成部分是:

class TouchTest extends Component {
  render() {
    return (
      <View style={styles.container}>
        <TouchBox style={{backgroundColor: 'skyblue', width: 200, height: 100, alignItems: 'center'}}/>
        <Text>Test</Text>
        <TouchBox style={{backgroundColor: 'skyblue', width: 200, height: 100, alignItems: 'center'}}/>
      </View>
    );
  }
}

Edit: I would like to be able to touch both of the counters simultaneously and have them work. 编辑:我希望能够同时触摸两个计数器并使它们工作。 Right now if I touch one it becomes the only responder and blocks all other touches while my finger is pressed. 现在,如果我触摸一个,它将成为唯一的响应者,并在我按下手指时阻止所有其他触摸。

View which is outside the TouchableNativeFeedback needs to return true, like this:- 在TouchableNativeFeedback之外的视图需要返回true,如下所示:

<View style={this.props.style} onStartShouldSetResponder={(e) => {this.setState({counter: this.state.counter + 1}) return true;}}>
          <Text style={{fontSize: 75}}>{this.state.counter}</Text>
</View>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM