Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly implement the UIGestureRecognizer state machine #2004

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ void HandleTouch(TouchStatus status, TouchInteractionStatus? interactionStatus =
return;
}

// We MUST implement the Apple state machine. This ensures that the interaction
// between multiple simultaneous touch recognizers works correctly.
// https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/implementing_a_custom_gesture_recognizer/about_the_gesture_recognizer_state_machine?language=objc
State = status switch
{
TouchStatus.Canceled => UIGestureRecognizerState.Failed,
TouchStatus.Completed => UIGestureRecognizerState.Recognized,
_ => UIGestureRecognizerState.Possible,
};

if (interactionStatus is TouchInteractionStatus.Started)
{
behavior.HandleUserInteraction(TouchInteractionStatus.Started);
Expand Down Expand Up @@ -231,19 +241,10 @@ public override bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRe
{
touchGesture.HandleTouch(TouchStatus.Canceled, TouchInteractionStatus.Completed);
touchGesture.isCanceled = true;
}

return true;
}

public override bool ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
if (recognizer.View.IsDescendantOfView(touch.View))
{
return true;
}

return touch.View.IsDescendantOfView(recognizer.View) && (touch.View.GestureRecognizers is null || touch.View.GestureRecognizers.Length == 0);
return false;
}
}
}
Expand Down