Created
February 14, 2024 18:55
-
-
Save mshivam019/714768c7b48aaf1e3283635b9bdd7089 to your computer and use it in GitHub Desktop.
Pan Handler for react native gesture handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useRef} from 'react'; | |
import {PanResponder} from 'react-native'; | |
import {GestureHandlerRootView} from 'react-native-gesture-handler'; | |
export default GestureHandlerContainer = ({onRightSwipe = () =>{}, onLeftSwipe = () =>{},onUpSwipe = () =>{},onDownSwipe = () =>{},children}) => { | |
const panResponder = useRef( | |
PanResponder.create({ | |
onStartShouldSetPanResponder: () => true, | |
onMoveShouldSetPanResponder: () => true, | |
onPanResponderRelease: (event, gestureState) => { | |
const x = gestureState.dx; | |
const y = gestureState.dy; | |
if (Math.abs(x) > Math.abs(y)) { | |
if (x >= 0) { | |
// console.log('swipe right'); | |
onRightSwipe(); | |
} else { | |
// console.log('swipe left'); | |
onLeftSwipe(); | |
} | |
} else { | |
if (y >= 0) { | |
// console.log('swipe down') | |
onDownSwipe(); | |
} else { | |
// console.log('swipe up') | |
onUpSwipe(); | |
} | |
} | |
}, | |
}), | |
).current; | |
return ( | |
<GestureHandlerRootView style={{flex: 1}} {...panResponder.panHandlers}> | |
{children} | |
</GestureHandlerRootView> | |
); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment