0

I'm developing an app which requires handling of simultaneous touches(usually 2 or 3 at most). So that the user can have multiple buttons pressed at the same time. The problem's that on Android when the user presses one of the buttons(not necessary RN's <Button>), they cannot press the others as long as the first button is hold. First I had tried RN's <Pressable>, to receive both PressIn and PressOut events, in a way like this:


const Key = ({label, ...}): Node => {
  const [hold, setHold] = useState(false);
  return (
    <Pressable
      onPressIn={...}
      onPressOut={...}
      style={hold ? styles.keyHold : styles.keyUnhold}
    >
      <Text>{label}</Text>
    </Pressable>
  );
};


const Keyboard = ({...}): Node => {
  //...
  return (
    <FlatList
      data={data}
      renderItem={({item}) => {
        const {label, name} = item;
        return <Key title={label} ... />;
      }}
    />
  );
};

And it had the said problem. Afterwards I had found many answers on StackOverflow(React Native - onTouchStart vs PanResponder for multiple touches per second How do I enable touch on multiple buttons simultaneously in react native? How to detect simultaneous onPress events in react native?) and I tried onTouchStart/onTouchEnd and <View> replacing <Pressable> and onPressIn/onPressOut. The problem remains except that when holding a button(constructed using <View>) and trying to press another one, the first one is released and any touch on the screen will trigger the first one(which is still physically hold).

Many answers and comments on the posted questions suggest using PanResponder but by ducking I cannot find any example of achieving something like what I want using PanResponder.

So the question is that: How to achieve this in RN? Should I use PanResponder? If yes, an example on how to do that can really be helpful for me. Or perhaps there are other ways of achieving this of which I'm unaware?

(I'm using RN 0.66 on a real Android device running 7.1)

  • What are you trying to achieve? When you say, "When a user is tapping a button, he cannot tap elsewhere" - does this mean you are looking for multi-tap features so that users can tap/press multiple elements at the same time ? – Software Dev Nov 28 '21 at 07:37
  • Yes I need to handle multiple taps at the same time for multiple elements/buttons. I've edited the question to clarify this. – Farooq Karimi Zadeh Nov 28 '21 at 08:51

1 Answers1

0

This dont work whit element Pressable or Touchable instead of this you can use element View with event onTouchStart and onTouchEnd

  <View>
               <View onTouchStart={onPressInFirst} onTouchEnd={onPressOutFirst}>
                  <Text>BUTTON 1</Text>
                </View>
                <View onTouchStart={onPressInSecond} onTouchEnd={onPressOutSecond}>
                  <Text>BUTTON 2</Text>
                </View>
</View>