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

GestureDetector not detecting gestures or not firing any events

Open ernestasg152 opened this issue 4 years ago • 10 comments

Description

I'm having trouble with getting a gesture handler to work. I'm using the new API (2.0) for gestures, so I'm using GestureDetector with a Gesture.Pan(), and I can't make any events fire for the gesture. I'm using Wix RN Navigation library, so this might be a cause of the issue?

Steps To Reproduce

  1. Register the screen component wrapping it with the provided HOC: Navigation.registerComponent(SCREENS.LOGIN.ROOT, () => gestureHandlerRootHOC(LoginScreen));

  2. The screen itself includes just one functional component which is present in the examples folder of the library:

`import React from 'react'; import { Button, StyleSheet, View } from 'react-native'; import { Gesture, GestureDetector } from 'react-native-gesture-handler'; import Animated, { useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated';

function Ball() { const isPressed = useSharedValue(false); const offset = useSharedValue({ x: 0, y: 0 }); const start = useSharedValue({ x: 0, y: 0 });

const animatedStyles = useAnimatedStyle(() => {
    console.log('ANIMATED STYLES --- ', offset.value.x, offset.value.y, isPressed.value);
    return {
        transform: [
            { translateX: withSpring(offset.value.x) },
            { translateY: withSpring(offset.value.y) },
            { scale: withSpring(isPressed.value ? 1.2 : 1) },
        ],
        backgroundColor: isPressed.value ? 'yellow' : 'blue',
    };
});

const gesture = Gesture.Pan()
    .onBegin(() => {
        'worklet';
        console.log('ONBEGIN');
        isPressed.value = true;
    })
    .onUpdate((e) => {
        'worklet';
        console.log('ONUPDATE');
        offset.value = {
            x: e.translationX + start.value.x,
            y: e.translationY + start.value.y,
        };
    })
    .onEnd(() => {
        'worklet';
        start.value = {
            x: offset.value.x,
            y: offset.value.y,
        };
    })
    .onFinalize(() => {
        isPressed.value = false;
    });

return (
    <>
        <GestureDetector gesture={gesture}>
            <Animated.View style={[styles.ball, animatedStyles]} />
        </GestureDetector>
        <Button
            title="Test"
            onPress={() => {
                isPressed.value = !isPressed.value;
                offset.value = {
                    x: isPressed.value ? Math.random() * 100 : 0,
                    y: isPressed.value ? Math.random() * 100 : 0,
                };
            }}
        />
    </>
);

}

export default function Example() { return ( ); }

const styles = StyleSheet.create({ container: { flex: 1, }, ball: { width: 100, height: 100, borderRadius: 100, backgroundColor: 'blue', alignSelf: 'center', }, }); `

I tweaked the code a bit to test if the animated value itself is working, which it is, so I struggle to get the gestures to get detected.

Expected behavior

Gesture events should be firing on the ball View, and the view should be moved to the new x/y coordinates

Actual behavior

No gesture events get fired in any of the worklets

Package versions

  • React: 17.0.2
  • React Native: 0.66.0
  • React Native Gesture Handler: 2.1.0
  • React Native Reanimated: 2.3.1
  • React Native Navigation: 7.22.3

ernestasg152 avatar Dec 31 '21 11:12 ernestasg152

Don't forget to wrap your app inside <GestureHandlerRootView> ... </GestureHandlerRootView>

ozgurbayram avatar Jan 02 '22 01:01 ozgurbayram

I wrapped the highest level with <GestureHandlerRootView>, as well as wrapping the screen with gestureHandlerRootHOC when registering it, but still didn't work. Solved it for now by just using <PanGestureHandler>

ernestasg152 avatar Jan 02 '22 10:01 ernestasg152

I'm using React Navigator. I had to wrap the screen with GestureHandlerRootView instead of View, and it worked. Wrapping at the root of App.tsx won't give me any view. I guess. It's supposed to be a View.

By the way, Expo documentations doesn't mention GestureHandlerRootView. Any Expo contributor coming across should take note to update that page out.

Also, the react-native-gesture-handler's Introduction page also does not mention this GestureHandlerRootView in the Expo section but the JS section. I mean, my understanding is that I use Expo and I do expo install, that's what it said. I don't look for anything else other than this Expo section. That's just confusing, not to mention Installation is not supposed to be in the Introduction page. I read Introduction to read about the project, and I go to Installation to install the library.

intagaming avatar Jan 02 '22 11:01 intagaming

Hi, I tried to reproduce this issue using the blank app with libraries in versions you mentioned and the tweaked code. While the gesture was not working without GestureHandlerRootView or gestureHandlerRootHOC, using either of them fixed the problem in my case. Could you prepare a repository with the reproduction?

j-piasecki avatar Jan 03 '22 11:01 j-piasecki

@j-piasecki Hi, that's weird since I tried using both, so I'm guessing some of my additional setup is causing this. I'll post a link when I prepare a repo for this.

ernestasg152 avatar Jan 03 '22 13:01 ernestasg152

In fact I tested with the example app and all the gestures are working with the new API, even when I tried removing the GestureHandlerRootView that's in the highest level in the root component. Which is kind of weird since for you it wasn't working without using one of the wrappers

ernestasg152 avatar Jan 03 '22 14:01 ernestasg152

@j-piasecki Created a reproduction repo for this, the installation steps are in the readme, if the run command doesn't work you might need to run it via XCode. In essence I added 2 views with both <GestureDetector> and <PanGestureHandler> approaches, and the first one is not working with both HOC options in place.

ernestasg152 avatar Jan 04 '22 14:01 ernestasg152

@j-piasecki Just figured out what was going on here, GestureDetector isn't working when JS debugging is turned on, once I tested with it off I noticed it started working as expected, kind of. I noticed the pan gesture velocities are always 0, so that's also kind of weird, might be because it's on simulator though.

ernestasg152 avatar Jan 04 '22 15:01 ernestasg152

@ernestasgobionis I checked it and it's not working for me with debugging as well. Thank you for taking the time to dig into this.

j-piasecki avatar Jan 05 '22 10:01 j-piasecki

I'm using React Navigator. I had to wrap the screen with GestureHandlerRootView instead of View, and it worked. Wrapping at the root of App.tsx won't give me any view. I guess. It's supposed to be a View.

By the way, Expo documentations doesn't mention GestureHandlerRootView. Any Expo contributor coming across should take note to update that page out.

Also, the react-native-gesture-handler's Introduction page also does not mention this GestureHandlerRootView in the Expo section but the JS section. I mean, my understanding is that I use Expo and I do expo install, that's what it said. I don't look for anything else other than this Expo section. That's just confusing, not to mention Installation is not supposed to be in the Introduction page. I read Introduction to read about the project, and I go to Installation to install the library.

Hi, few issues down found this solution that might work for you. #1478 (comment)

And by the way, you seem to have great initiative to improve docs, maybe you should contribute to those opensource projects :)

jasselmar avatar Feb 04 '22 22:02 jasselmar