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

[Touchable] How to stopPropagation touch event

Open magicismight opened this issue 11 years ago • 33 comments

I have a ScrollView which contains a View inside. When I move touch on the View, It will scroll the ScrollView too. I woner is there a way to stopPropagation just like the DOM event does. I have tried to set PanResponder like this,but It didn`t work

PanResponder.create({
  onStartShouldSetPanResponder:  function () {
    return false;
  },
  onStartShouldSetPanResponderCapture: function () {
    return false;
  },
  onMoveShouldSetPanResponder:  function () {
    return false;
  },
  onMoveShouldSetPanResponderCapture: function () {
    return false;
  },
  onPanResponderTerminationRequest: function () {
    return false;
  }
});

magicismight avatar Apr 28 '15 08:04 magicismight

Having the same issue. I'm wondering if ShouldCapture events should return true if the movement is more horizontal than vertical. The documents are not great for the PanResponder API.

joewood avatar May 30 '15 00:05 joewood

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 04 '15 18:08 ghost

PanResponder.create({ will return an object, and you need to render a view with the responder handlers on it. For exmple:

componentWillMount: function() {
  this.gesture = PanResponder.create({ ... });
}
render: function() {
  return (
    <View {...this.gesture.panHandlers} ... />
  );
},

preventDefault does not enable the touch responsiveness we want on native mobile. To make actions clear to the user, components should visibly respond for a given touch, and give up responder if something else needs it. Although the touch responder API isn't perfect, it should work for most things.

Can you be more specific about what you are trying to do? Maybe post a minimal example of the issue on rnplay.org?

If you have any improvements to the gesture guide or the pan responder docs, feel free to make a pull request!

ericvicenti avatar Aug 04 '15 20:08 ericvicenti

@ericvicenti - I've had this issue myself as well - for example:

screen shot 2015-09-10 at 10 43 13 am

Inside of the horizontal ScrollView, at the bottom of each page there is a footer that can be pulled upwards. If you start pulling this up (onStartShouldSetPanResponder returns true) then the ScrollView should not do respond to any movement. The only solution I have for this right now is to lock the ScrollView when the gesture begins, and unlock it when it's released. There is a slight problem with this solution still because if you do just the right gesture so that it scrolls the ScrollView a little bit before setting the responder and locking the ScrollView, you end up in a weird state where you're locked in between pages. To handle this, I need to track the active page in the ScrollView and scroll it back to the right position when I lock it.

The best solution would be to just be able to have the PanResponder become responder and block ScrollView. @vjeux mentioned that on Android there is a ~5px delay when the gesture starts before the ScrollView takes over, which gives the PanResponder a chance to respond. Maybe a similar solution is needed for iOS.

brentvatne avatar Sep 10 '15 18:09 brentvatne

While trying to move a SliderIOS's knob, Navigator starts the swipe back gesture at the same time. Using PanResponder to catch the touch start doesn't seem to help.

propagation-stop-bug

componentWillMount: function() {
    this._panResponder = PanResponder.create({
        onStartShouldSetPanResponder: (evt, gestureState) => true,
        onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
        onMoveShouldSetPanResponder: (evt, gestureState) => true,
        onMoveShouldSetPanResponderCapture: (evt, gestureState) => true
    });
}
<SliderIOS {...this._panResponder.panHandlers} />

Even though I use PanResponder to capture the touch event, the onMoveShouldSetPanResponder callback gets called in Navigator (https://github.com/facebook/react-native/blob/b9c5f753aaac7b387e671e0df3cef4cc19c088f4/Libraries/CustomComponents/Navigator/Navigator.js#L612-L614).

jukkatupamaki avatar Jan 09 '16 22:01 jukkatupamaki

uiexplorer-slider-bug

I just noticed that the same bug exists in the UIExplorer example.

jukkatupamaki avatar Jan 10 '16 13:01 jukkatupamaki

I had the same problem. This response solved it for me. Essentially same as @tukkajukka's solution above, only with

componentWillMount () {
  this._panResponder = PanResponder.create({
    onPanResponderTerminationRequest: () => false,
    onStartShouldSetPanResponderCapture: () => true,
  });
}

casperin avatar Jan 29 '16 13:01 casperin

I also want to do same thing(Stop Panresponder after a given event).Please Help

rameshvishnoi90904 avatar Mar 03 '16 13:03 rameshvishnoi90904

+1

gran33 avatar Mar 20 '16 15:03 gran33

+1

dionysiusmarquis avatar Apr 04 '16 10:04 dionysiusmarquis

Hi , I do have the same issue , I am using pan gesture to a view , which is children of a scroll view. When I drag that view, scroll view drag itself.

I tried using scrollEnabled property , but callback goes into infinite loop.

ezgif com-video-to-gif

RD1991 avatar May 06 '16 11:05 RD1991

+1

getnashty avatar May 24 '16 03:05 getnashty

+1

bentomas avatar Jun 21 '16 19:06 bentomas

Ideally, once something gets the responder, it can prevent others from responding by setting onPanResponderTerminationRequest: () => false,

However, we often have troubles while interacting with touch interactions happening on the main thread, like the iOS slider, or any scroll views. The problem is that javascript cannot immediately reject the responsiveness of the native event, because the main thread must synchronously decide if JS or the scroll view should become responder, and JS can only respond asynchronously.

This is a difficult problem to solve, and is something we have been thinking about for a while. cc @vjeux @sahrens @jordwalke @lelandrichardson. This could be solved in a similar way as @kmagiera is moving animations to the UI thread. Basically we construct and serialize some logic such that these immediate operations can be resolved without waiting for the JS thread.

ericvicenti avatar Jun 21 '16 19:06 ericvicenti

A workaround for now could be to disable ScrollView onPanResponderGrant then enable onPanResponderRelease

superandrew213 avatar Jul 05 '16 00:07 superandrew213

+1

We tried disabling the ScrollView onPanResponderGrant, but if you pan the View (children) too fast, the pan propagates to the ScrollView before the block, so it moves with it.

mjracca avatar Aug 01 '16 09:08 mjracca

For everyone having this issue, you can try our current hack (https://github.com/mjracca/react-native-scroll-block).

Blocking the ScrollView wasn't an option for us, so we created a View that doesn't propagate pointer events, without having to block the scroll (basically a UIView that has a UIPanGestureRecognizer)

We also added a blocked prop, because we need that View to be able to propagate the pointer sometimes.

Hope it helps!

mjracca avatar Aug 04 '16 23:08 mjracca

Hi , I do have the same issue , I am using pan gesture to a view , which is children of a scroll view. When I drag that view, scroll view drag itself.

uc-spr avatar Sep 07 '16 06:09 uc-spr

+1

dev-xdyang avatar Sep 30 '16 06:09 dev-xdyang

+1

kiprijonas avatar Sep 30 '16 16:09 kiprijonas