-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuxiliaryPreviewBasicUsage01Controller.swift
128 lines (104 loc) · 3.52 KB
/
AuxiliaryPreviewBasicUsage01Controller.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// AuxiliaryPreviewBasicUsage01Controller.swift
// ContextMenuAuxiliaryPreviewExample
//
// Created by Dominic Go on 11/23/23.
//
import UIKit
import ContextMenuAuxiliaryPreview
class AuxiliaryPreviewBasicUsage01Controller: UIViewController, ContextMenuManagerDelegate {
var interaction: UIContextMenuInteraction?;
var contextMenuManager: ContextMenuManager?;
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white;
let boxView: UIView = {
let view = UIView();
view.backgroundColor = .systemPink;
view.layer.cornerRadius = 10;
let interaction = UIContextMenuInteraction(delegate: self);
self.interaction = interaction;
view.addInteraction(interaction);
let contextMenuManager = ContextMenuManager(
contextMenuInteraction: interaction,
menuTargetView: self.view
);
self.contextMenuManager = contextMenuManager;
contextMenuManager.delegate = self;
contextMenuManager.auxiliaryPreviewConfig = AuxiliaryPreviewConfig(
verticalAnchorPosition: .automatic,
horizontalAlignment: .targetCenter,
preferredWidth: .constant(100),
preferredHeight: .constant(100),
marginInner: 10,
marginOuter: 10,
transitionConfigEntrance: .syncedToMenuEntranceTransition(),
transitionExitPreset: .fade
);
return view;
}();
boxView.translatesAutoresizingMaskIntoConstraints = false;
self.view.addSubview(boxView);
NSLayoutConstraint.activate([
boxView.centerXAnchor.constraint(
equalTo: self.view.centerXAnchor
),
boxView.centerYAnchor.constraint(
equalTo: self.view.centerYAnchor
),
boxView.widthAnchor.constraint(
equalToConstant: 100
),
boxView.heightAnchor.constraint(
equalToConstant: 100
),
]);
};
};
extension AuxiliaryPreviewBasicUsage01Controller: UIContextMenuInteractionDelegate {
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
configurationForMenuAtLocation location: CGPoint
) -> UIContextMenuConfiguration? {
self.contextMenuManager!.notifyOnContextMenuInteraction(
interaction,
configurationForMenuAtLocation: location
);
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ -> UIMenu? in
let shareAction = UIAction(
title: "Share",
image: UIImage(systemName: "square.and.arrow.up")
) { _ in
// no-op
};
return UIMenu(title: "", children: [shareAction]);
};
};
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willDisplayMenuFor configuration: UIContextMenuConfiguration,
animator: UIContextMenuInteractionAnimating?
) {
self.contextMenuManager!.notifyOnContextMenuInteraction(
interaction,
willDisplayMenuFor: configuration,
animator: animator
);
};
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willEndFor configuration: UIContextMenuConfiguration,
animator: UIContextMenuInteractionAnimating?
) {
self.contextMenuManager!.notifyOnContextMenuInteraction(
interaction,
willEndFor: configuration,
animator: animator
);
};
func onRequestMenuAuxiliaryPreview(sender: ContextMenuManager) -> UIView? {
let menuAuxiliaryPreview = UIView(frame: .zero);
menuAuxiliaryPreview.backgroundColor = .red;
return menuAuxiliaryPreview;
};
};