question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GestureDetector not detecting gestures or not firing any events

See original GitHub issue

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 ( <View style={styles.container}> <Ball /> </View> ); }

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ozgurbayramcommented, Jan 2, 2022

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

1reaction
j-piaseckicommented, Jan 5, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flutter- GestureDetector not working with containers in stack
I have two containers in a stack and both containers have GestureDetector.The OnTap for the first container is working fine ...
Read more >
GestureDetector class - widgets library
A widget that detects gestures. Attempts to recognize gestures that correspond to its non-null callbacks. If this widget has a child, it defers...
Read more >
GestureDetector | React Native Gesture Handler
It is responsible for creating and updating native gesture handlers based on the config of provided gesture. The most significant difference between it...
Read more >
Detect common gestures
This is because all gestures begin with an onDown() message. If you return false from onDown() , as GestureDetector.SimpleOnGestureListener does ...
Read more >
GestureDetector In Flutter
If it does not have a child, it fattens to fit the parent instead. By default a Gesture Detector with an unseeable child...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found