简体   繁体   中英

react-native PanResponder: OnMoveShouldSetPanResponder Not Working

I am working on a small project using React-Native PanResponder.

My Goal: I am trying to capture if the user is touching the screen, not moving around.

My Problem: I cannot run any code inside onMoveShouldSetPanResponder(evt, gestureState) in order to block PanResponder when the user only touches.

I tried console.log the param gestureState or a random string and it did not response.

my code:

    componentWillMount(){

        this._panResponder = PanResponder.create({

        onStartShouldSetPanResponderCapture: () => true,

        onStartShouldSetPanResponder: (evt, gestureState) => true,

        onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,

        onMoveShouldSetPanResponder: (evt, gestureState) => {
            // no console.log shown in this scope.
            console.log(gestureState);
            console.log("a string inside should set on move")
            // the PanResponder still works despite this returns false
            return false;
        },

        onPanResponderMove : (evt, gState) => {
            // this works perfectly. 
            this.setState({addAngle: this.state.addAngle + gState.vy*40});
        },

    })

}

My Question is: Am I missing anything here ? I saw the code snippets of others and they seem to be working very well.

where I saw the sample code:https://gist.github.com/teameh/dd055d546a3bd8f85b9516840e3a45f3

Tech:

  • react-native cli (no expo)

  • android API version 23 from Android Emulator (Android Studio)

Note: I have not tested on any Iphone device yet.

Cheers !

I solved this problem by deleting onStartShouldSetPanResponder. I can't point to any documentation to support this but I believe that onStartShouldSetPanResponder and onMoveShouldSetPanResponder are mutually exclusive.

In my case it was onMoveShouldSetPanResponderCapture that was interfering with onMoveShouldSetPanResponder, but it does capture too so that kinda makes sense.

One extra tip to bare in mind when you are debugging this stuff - remember that if you create your PanResponder using useRef, you will need to unmount and mount before your changes take effect.

Obvious i know - but it took me a while before i realised why my changes were not fast refreshing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM