简体   繁体   English

当按下另一个可触摸的不透明度时,一个可触摸的不透明度不会按下

[英]One touchable opacity doesn't press when another touchable opacity is pressed

I was making a program in React native in which there are two buttons which when pressed will increase a tap counter.我在 React native 中制作了一个程序,其中有两个按钮,按下时会增加一个点击计数器。 The problem is when one button is pressed the other won't respond and doesn't increase the counter.问题是当一个按钮被按下时,另一个按钮不会响应并且不会增加计数器。 The button is made with Touchableopacity and the action is done by onPress.按钮由 Touchableopacity 制成,动作由 onPress 完成。

<View style = {{flex:1,flexDirection:'row',justifyContent:'center'}}>
            <View>
            <TouchableOpacity style = {styles.button}
            onPress = {() => this.start() }
            >
              <Text style={styles.buttonText}>
                Tap
              </Text>
            </TouchableOpacity>
            </View>
            <View>
            <TouchableOpacity style = {styles.button}
            onPress = {() => this.start() }>
              <Text style={styles.buttonText}>
                Tap
              </Text>
            </TouchableOpacity>
            </View>
          </View>
</View>

The problem i resolved by using and onTouchStart instead of and onPress我通过使用和 onTouchStart 而不是和 onPress 解决的问题

Try using onTouchStart instead of onPress .尝试使用onTouchStart而不是onPress

So change:所以改变:

<TouchableOpacity style={styles.button} onPress={() => this.start()}>

To:至:

<TouchableOpacity style={styles.button} onTouchStart={() => this.start()}>

The reason you want to to this is because while one button is still being pressed, the onPress action on another button won't fire until you stop pressing the original button.您想要这样做的原因是,当一个按钮仍然被按下时,另一个按钮上的onPress操作不会触发,直到您停止按下原始按钮。

I don't know what version of React Native the previous posters were using but "onTouchStart" is only available on View components not on Touchable Opacity.我不知道以前的海报使用的是什么版本的 React Native,但“onTouchStart”仅适用于 View 组件,而不适用于 Touchable Opacity。

Wrapping my touchable in a view mimics onPress behavior very well将我的可触摸对象包装在视图中可以很好地模仿 onPress 行为

<View onTouchStart={myEventHandler}>
  <TouchableOpacity>
    <Text>My Touchable Button</Text>
  </TouchableOpacity>
</View>

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

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