Inspections of UIContextMenuInteractionDelegate Calls

yosshi4486
2 min readOct 13, 2021

I will use simple xcodeproj, which has only one view in center.

The blue view is laid out in the center of parent view, and it have 300 * 300 pt size. I use addInteraction(_:) method to add UIContextMenuInteraction instance to the blue view.

The source code is here.

Add context menu interaction and delegates.

In this sample project, I've implemented share and openInSafari actions for UIContextMenuConfiguration.

and then, adopt all other delegate methods.

Inspection 1: Show menus, the tap the preview.

Outputs:

1.contextMenuInteraction(_:configurationForMenuAtLocation:)
2.contextMenuInteraction(_:previewForHighlightingMenuWithConfiguration:)
3.contextMenuInteraction(_:willDisplayMenuFor:animator:)
4.contextMenuInteraction(_:willPerformPreviewActionForMenuWith:animator:)
5.contextMenuInteraction(_:willEndFor:animator:)
6.contextMenuInteraction(_:previewForDismissingMenuWithConfiguration:)

Inspection 2: Show menus, then cancel the interaction.

Outputs:
1.contextMenuInteraction(_:configurationForMenuAtLocation:)
2.contextMenuInteraction(_:previewForHighlightingMenuWithConfiguration:)
3.contextMenuInteraction(_:willDisplayMenuFor:animator:)
4.contextMenuInteraction(_:willEndFor:animator:)
5.contextMenuInteraction(_:previewForDismissingMenuWithConfiguration:)

Inspection3: Start long press, but cancel it before a preview appears. (failed to show context menu)

Outputs:
1.contextMenuInteraction(_:configurationForMenuAtLocation:)
2.contextMenuInteraction(_:previewForHighlightingMenuWithConfiguration:)
3.contextMenuInteraction(_:willEndFor:animator:)

Note: In some cases, `previewForHighlightingMenuWithConfiguration` is also ignored in this flow.

Wrapup

Context menu delegate follows flow-chart bellow:

・Context menu basically goes straight for left chart.

・`previewForDismissingMenuWithConfiguration` is not called if user don't continue long press and it ends.

・`willPerformPreviewActionMenu` is called only if user tap a preview of context menu.

--

--

yosshi4486
0 Followers

Solo Application Developer of Apple Platform