I am building a game with React Native. In order to make a move in one of the four cardinal directions, the player must swipe anywhere on the screen. Up until now, I have achieved this with the following code, which records the XY position where the gesture begins and then does some calculations based on that when the gesture ends.
<Pressable style={styles.container}
onPressIn={e => {
touchPos.current.y = e.nativeEvent.pageY;
touchPos.current.x = e.nativeEvent.pageX;
}}
onPressOut={e => {
const minDist = 50;
const vertDist = touchPos.current.y - e.nativeEvent.pageY;
const horizDist = touchPos.current.x - e.nativeEvent.pageX;
// Then do some math and pass the swipe information on to game logic
}}>
{/* Some game components go here. */}
</Pressable>
This works fine, but it would be nice if when the user swipes in some direction but does not lift their finger, the game would repeatedly do moves in that direction (every 500ms for example) until they lift their finger. Unfortunately onLongPress
will not fire if the user presses and holds in a swipe motion. It only fires if one presses and holds without moving their finger.
I have scoured the web but perhaps I am not using the correct terminology to define my question as I cannot find any mentions of similar issues. The React Native documentation on Pressable isn't much help either. There is a property pressRetentionOffset
for how far outside a view a touch can still count as a press but nothing for how much a touch can move inside a view and still trigger onLongPress
.
While it would be a suitable solution to figure out onLongPress, all I really need is a way of continuously checking the X and Y position of a touch on an element being touched.