Skip to content

Commit

Permalink
Fix double start when activateAfterLongPress is used (#2628)
Browse files Browse the repository at this point in the history
## Description

It seems like native iOS recognizers (or at least Pan) don't respect
when their state is changed from outside, which caused a problem with
`.activateAfterLongPress` - before sending event in `Active` state, the
state of the native recognizer is updated to
`UIGestureRecognizerStateBegan`. Despite this, after moving the finger
it tries to change the state to `UIGestureRecognizerStateBegan` once
more, causing weird `Active` -> `Began`, `Began` -> `Active` chain.

This PR adds a simple check to detect the first event and stop
processing in case it happens.

~~One thing that *could* be related is the fact that when the gesture
fails before activation, the gesture stays in the `Began` state until
the finger is lifted. This still needs to be investigated.~~ I think
this is a separate issue.

Fixes
#2620

## Test plan

Tested on the example app.

---------

Co-authored-by: Michał Bert <[email protected]>
  • Loading branch information
j-piasecki and m-bert authored Jan 10, 2024
1 parent 7581fde commit 14e7ac0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions apple/RNGestureHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ - (void)sendEventsInState:(RNGestureHandlerState)state
return;
}

// Recognizers don't respect manually changing their state (that happens when we are activating handler
// under custom conditions). If we send a custom event in state ACTIVE and the recognizer will later update its
// state, we will end up sending ACTIVE->BEGAN and BEGAN->ACTIVE chain. To prevent this, we simply detect the first
// weird state change and stop it (then we don't update _lastState), so the second call ends up without state change
// and is fine.
if (state == RNGestureHandlerStateBegan && _lastState == RNGestureHandlerStateActive) {
return;
}

if (state == RNGestureHandlerStateActive) {
// Generate a unique coalescing-key each time the gesture-handler becomes active. All events will have
// the same coalescing-key allowing RCTEventDispatcher to coalesce RNGestureHandlerEvents when events are
Expand Down

0 comments on commit 14e7ac0

Please sign in to comment.