react-native-gesture-handler icon indicating copy to clipboard operation
react-native-gesture-handler copied to clipboard

detect when stop touching without moving

Open F4b1n0u opened this issue 7 years ago • 10 comments

Hi guys, I try to detect when a finger stops touching the screen especially in the case it didn't move the finger at all

it seems that only PanGestureHandler can do it due to in continuous nature, of course if I move I do know when the the State.END is triggered

basically I try to detect the touch, not the tap of the long press (which are )but just when the touch stops

F4b1n0u avatar Dec 19 '18 11:12 F4b1n0u

@F4b1n0u Yes, you should use PanGestureHandler. You can look into Example App, there are a bunch of samples on how to do it right :). If you have some code snippet I can look into and help you get proper behaviour :)

Actually, there is always some movement event from UI.

ice-chillios avatar Dec 19 '18 11:12 ice-chillios

sure thing !, here it is :) https://github.com/F4b1n0u/gesture-touch-release

it's an expo project so you can run it pretty easily I guess :)

F4b1n0u avatar Dec 19 '18 11:12 F4b1n0u

so if you long press the hold me button some other button appears, if you drag toward them and release your finger, there disappear, which is the expected behaviour, but if you hold and release without dragging at all, they stay visible, which is not what I want :S

F4b1n0u avatar Dec 19 '18 11:12 F4b1n0u

At the end of the day, I just need a callback from PanGestureHandler who is trigger when the touch is over regardless of the mouvement done.

and it seems to me that onEnded is triggered only if you moved the finger in the first place :s like the documentation says

"The handler activates when finger is placed on the screen and moved by some initial distance"

is there something I don't get properly ?

F4b1n0u avatar Dec 19 '18 13:12 F4b1n0u

just in case someone is having a look at the code I pushed, I just did clean it some obsolete stuff were still there, it should be cleaner now :)

F4b1n0u avatar Dec 19 '18 13:12 F4b1n0u

Hi !, I think I found a way to do it :)

actually it seems that we cannot do it while ONLY using a PanGestureHandler, but if you wrap it with a TapGestureHandler or a LongPressGestureHandler in my case, the onEnd of the wrapper is triggered when you stop touching and even if you do not move at all :)

there is maybe a better way to do it, but it sorted my issue :)

F4b1n0u avatar Dec 27 '18 18:12 F4b1n0u

This issue appears to be solved. Thanks everyone!

osdnk avatar Jan 04 '19 13:01 osdnk

how did you ended up doing it? @F4b1n0u

i tried this but the release (pressEnd) is not called when the finger is not moving

import React from 'react';
import { View, Text } from 'react-native';
import { PanGestureHandler, State } from 'react-native-gesture-handler';

const RecordButton = () => {
  const onGestureEvent = (event) => {
    if (event.nativeEvent.state === State.BEGAN) {
      console.log('Press started');
      // Start recording logic here
    } else if (event.nativeEvent.state === State.END) {
      console.log('Press ended');
      // Stop recording logic here
    }
  };

  return (
    <PanGestureHandler
      onGestureEvent={onGestureEvent}
      onHandlerStateChange={onGestureEvent}>
      <View style={{ width: 70, height: 70, backgroundColor: 'red' }}>
        <Text style={{ textAlign: 'center', marginTop: 25 }}>Record</Text>
      </View>
    </PanGestureHandler>
  );
};

export default RecordButton;

bryanprimus avatar Nov 30 '23 05:11 bryanprimus

noticed that the state event.nativeEvent.state changes to 1 if we are not moving our finger while releasing it.

image image

ended up doing this

const handleGestureEvent = useCallback(
    (event: GestureEvent<PanGestureHandlerEventPayload>) => {
      if (event.nativeEvent.state === State.BEGAN) {
        startRecording();
      } else if (
        event.nativeEvent.state === State.END ||
+        event.nativeEvent.state === State.FAILED
      ) {
        stopRecording(startPlayback);
      }
    },
    [startPlayback, startRecording, stopRecording],
  );

but not sure if there is a drawback

bryanprimus avatar Nov 30 '23 05:11 bryanprimus