-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Dimensions, GestureResponderEvent } from 'react-native' | ||
|
||
const windowWidth = Dimensions.get('window').width | ||
|
||
export function useSwipe( | ||
onSwipeLeft?: ((event: GestureResponderEvent) => void) | undefined, | ||
onSwipeRight?: ((event: GestureResponderEvent) => void) | undefined, | ||
rangeOffset = 4, | ||
) { | ||
let firstTouch = 0 | ||
|
||
// set user touch start position | ||
function onTouchStart(e: GestureResponderEvent) { | ||
firstTouch = e.nativeEvent.pageX | ||
} | ||
|
||
// when touch ends check for swipe directions | ||
function onTouchEnd(e: GestureResponderEvent) { | ||
// get touch position and screen size | ||
const positionX = e.nativeEvent.pageX | ||
const range = windowWidth / rangeOffset | ||
|
||
// check if position is growing positively and has reached specified range | ||
if (positionX - firstTouch > range) { | ||
if (onSwipeRight) onSwipeRight(e) | ||
} | ||
// check if position is growing negatively and has reached specified range | ||
else if (firstTouch - positionX > range) { | ||
if (onSwipeLeft) onSwipeLeft(e) | ||
} | ||
} | ||
|
||
return { onTouchStart, onTouchEnd } | ||
} |