Skip to content

Commit

Permalink
Reset relevant handler when recognizer gets reset. (#2705)
Browse files Browse the repository at this point in the history
## Description

At the moment `reset` method is only called in
`gestureRecognizerShouldBegin`:
https://github.com/software-mansion/react-native-gesture-handler/blob/35ec17d636220c1ad4226456aa3b03c846ba16a8/apple/RNGestureHandler.m#L487
However, I've noticed that this method is called after some events are
already sent, mainly the `BEGIN` event, which is often sent as soon as
`onTouchesBegan` is executed for the first pointer.

According to the [apple
documentation](https://developer.apple.com/documentation/uikit/uigesturerecognizer/1620004-reset?language=objc),
the `reset` method of recognizer is the place to reset the internal
state of recognizer, which in this case in my opinion should also
include the state of the handler.

The issue this fixes is the one described in
#2628:
> 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.

This was coming from the fact that the handler was failing before it was
allowed to 'begin' (iOS begin not RNGH begin), so
`gestureRecognizerShouldBegin` was not called, thus not resetting the
failing handler. Because of that, it was sending events containing wrong
states.

## Test plan

Tested on the Example app.
  • Loading branch information
j-piasecki authored Jan 10, 2024
1 parent 7c115be commit 7581fde
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions apple/Handlers/RNFlingHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ - (void)reset
[_gestureHandler.pointerTracker reset];
_hasBegan = NO;
[super reset];
[_gestureHandler reset];
}

- (CGPoint)getLastLocation
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNForceTouchHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ - (void)reset
{
[_gestureHandler.pointerTracker reset];
[super reset];
[_gestureHandler reset];
_force = 0;
_firstTouch = NULL;
}
Expand Down
6 changes: 6 additions & 0 deletions apple/Handlers/RNHoverHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ - (void)cancel
self.enabled = NO;
}

- (void)reset
{
[super reset];
[_gestureHandler reset];
}

- (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction styleForRegion:(UIPointerRegion *)region
{
if (interaction.view != nil && _hoverEffect != RNGestureHandlerHoverEffectNone) {
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNLongPressHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ - (void)reset
[_gestureHandler.pointerTracker reset];

[super reset];
[_gestureHandler reset];
}

- (NSUInteger)getDuration
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNManualHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ - (void)reset
{
[_gestureHandler.pointerTracker reset];
[super reset];
[_gestureHandler reset];

_shouldSendBeginEvent = YES;
}
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNNativeViewHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ - (void)reset
{
[_gestureHandler.pointerTracker reset];
[super reset];
[_gestureHandler reset];
}

@end
Expand Down
2 changes: 2 additions & 0 deletions apple/Handlers/RNPanHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ - (void)interactionsMoved:(NSSet *)touches withEvent:(UIEvent *)event

if (self.state == UIGestureRecognizerStatePossible && [self shouldFailUnderCustomCriteria]) {
self.state = UIGestureRecognizerStateFailed;
[self triggerAction];
return;
}

Expand Down Expand Up @@ -218,6 +219,7 @@ - (void)reset
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(activateAfterLongPress) object:nil];
self.enabled = YES;
[super reset];
[_gestureHandler reset];
}

- (void)updateHasCustomActivationCriteria
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNPinchHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ - (void)reset
{
[_gestureHandler.pointerTracker reset];
[super reset];
[_gestureHandler reset];
}

@end
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNRotationHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ - (void)reset
{
[_gestureHandler.pointerTracker reset];
[super reset];
[_gestureHandler reset];
}

@end
Expand Down
1 change: 1 addition & 0 deletions apple/Handlers/RNTapHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ - (void)reset
_maxNumberOfTouches = 0;
self.enabled = YES;
[super reset];
[_gestureHandler reset];
}

@end
Expand Down

0 comments on commit 7581fde

Please sign in to comment.