Where are the gesture recognizers for the iOS 13 undo/redo gestures?

Viewed 1507

New in iOS 13, three gestures are interpreted as asking for undo and redo:

  • Double tap with three fingers means undo.

  • Swipe left with three fingers means undo.

  • Swift right with three fingers means redo.

Where are the gesture recognizers that detect these gestures? What would I do to override them, turn them off, or arbitrate between them and my own gestures?

2 Answers

Looks like there's a private class called UIUndoGestureRecognizer that is attached to a UIEditingOverlayGestureView which lives in a UIInputSetContainerView inside a UITextEffectsWindow. (To figure this out, I put a breakpoint inside an UndoManager's registerUndo handler and tried swiping left with three fingers, then poked around in the resulting stack trace and assembly code until I found the gesture recognizer instance.)

You could probably use the methods in UIGestureRecognizerDelegate to arbitrate between your gesture recognizers and these system ones, and you could override editingInteractionConfiguration in your view controller or view to return .none in order to just turn them off completely.

To detect when user performs the undo/redo gestures:

It is unified through the system, So it is completely free if you are using UndoManager(More details) or NSUndoManager. So if you implement it like the use shakes, it works for the gesture too.

To change the behavior of the gesture (to prevent conflicting with other gestures of the app)

You can change the configuration of it with this variable:

public protocol UIResponder {
    var editingInteractionConfiguration: UIEditingInteractionConfiguration
}

it is an enum:

public enum UIEditingInteractionConfiguration {
    case `default` // System behavior 
    case none // Disable
}

So it would be disable like:

override var editingInteractionConfiguration: UIEditingInteractionConfiguration { .none }

Sorce:

WWDC 2019 - Modernizing Your UI for iOS 13 - 37:00

Note that there are bunch of more gestures in iOS 13, for example pinching 3 fingers in will copy the selected text and pinch them out will paste it. All of them connected to the old style actions (for example the classic shake action) alongside with this protocol and you can override it in any responder.

Apple call all of these Editing Gestures and you can see the session here

Note:

Some of the information in that session is not purely true, (for example UIResponder is not a protocol, it's a class actually) as @matt mentioned in the comments. But I have added the source of this answer and you can judge yourself.

Related