Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining Pan Gesture and Swipeable creates unexpected behavior #2862

Closed
Athe81 opened this issue Apr 12, 2024 · 4 comments
Closed

Combining Pan Gesture and Swipeable creates unexpected behavior #2862

Athe81 opened this issue Apr 12, 2024 · 4 comments
Labels
Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snack or repo is provided

Comments

@Athe81
Copy link

Athe81 commented Apr 12, 2024

Description

I have a GestureDetector with a pan gesture wraped around a Swipeable element. After wrapping the GestureDetector around the Swipeable element, the Swipeable Element does not work as expected. Before it opened or closed after releasing the finger. Now it stucks in the position.

Steps to reproduce

  1. Create a Swipeable element
  2. Wrap it with a GestureDetector (gesture pan)
  3. Test it

Snack or a link to a repository

https://snack.expo.dev/@bigbart/bugreport-swipeable-list

Gesture Handler version

2.14.0

React Native version

0.73.6

Platforms

iOS

JavaScript runtime

Hermes

Workflow

Expo managed workflow

Architecture

None

Build type

None

Device

None

Device model

No response

Acknowledgements

Yes

@github-actions github-actions bot added Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snack or repo is provided labels Apr 12, 2024
@m-bert
Copy link
Contributor

m-bert commented Apr 16, 2024

Hi @Athe81! Why do you want to wrap Swipeable with GestureDetector? Swipeables already have gesture handlers under the hood. Moreover, they use old API of gesture handler along with Animated API, which is not supported by GestureDetector (as you can read in the docs).

@Athe81
Copy link
Author

Athe81 commented Apr 16, 2024

Hi @m-bert!

Why do you want to wrap Swipeable with GestureDetector? Swipeables already have gesture handlers under the hood.

I want to give a haptic feedback to the user, if the Swipeable item is swiped more than a threshold. This is to signalize the user, that the action behind, will be executed if he releases his finger.
I don't know how to do this without GestureDetector.

@m-bert
Copy link
Contributor

m-bert commented Apr 17, 2024

I see. In that case you can try something like this:

const Item = ({ id, title }) => {
  const windowWidth = Dimensions.get('window').width;
  const threshold = windowWidth / 3;
  const hasReachedThreshold = useSharedValue(false);
  const panRef = useRef();

  const panGesture = Gesture.Pan()
    .onUpdate((e) => {
      hasReachedThreshold.value = Math.abs(e.translationX) >= threshold;
      console.log(hasReachedThreshold.value);
    })
    .withRef(panRef);

  const renderLeftActions = (progress, dragX) => {
    return <View style={styles.left} />;
  };

  const renderRightActions = (progress, dragX) => {
    return <View style={styles.right} />;
  };

  return (
    <GestureDetector gesture={panGesture}>
      <View>
        <Swipeable
          key={id}
          simultaneousHandlers={panRef}
          renderLeftActions={renderLeftActions}
          renderRightActions={renderRightActions}
          leftThreshold={threshold}
          rightThreshold={threshold}>
          <View style={styles.item}>
            <Text style={styles.text}>{title}</Text>
          </View>
        </Swipeable>
      </View>
    </GestureDetector>
  );
};

I've made a few changes in your code:

  1. I've wrapped Swipeable with View so that GestureDetector won't be using the same view as swipeable component
  2. I've added withRef so you can mark Pan as simultaneous with gesture handlers inside Swipeable

With these changes I was able to achieve what I believe is what you're trying to do. Let me know if it helps!

@Athe81
Copy link
Author

Athe81 commented Apr 17, 2024

It helped a lot. It works perfect now. Thanks a lot.
I didn't know the simultaneousHandlers

@Athe81 Athe81 closed this as completed Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snack or repo is provided
Projects
None yet
Development

No branches or pull requests

2 participants