ContextMenuAuxiliaryPreview
一个库,用于将辅助预览添加到上下文菜单预览的边缘。
- 支持使用编程方式显示上下文菜单。
- 预设置辅助预览进入/退出过渡。
- 支持将辅助预览作为弹出窗口显示。
- 此库在
react-native-ios-context-menu
中使用。
安装
Cocoapods
ContextMenuAuxiliaryPreview
通过 CocoaPods 提供。安装它,只需将以下行添加到您的 Podfile
pod 'ContextMenuAuxiliaryPreview'
Swift包管理器(SPM)
方法1:通过Xcode图形界面
- 文件 > Swift包 > 添加包依赖
- 添加
https://github.com/dominicstop/ContextMenuAuxiliaryPreview.git
方法2:通过Package.swift
- 打开您的项目
Package.swift
文件。 - 在
Package.swift
中更新dependencies
,并添加以下内容
dependencies: [
.package(url: "https://github.com/dominicstop/ContextMenuAuxiliaryPreview.git",
.upToNextMajor(from: "0.3.0"))
]
基本用法
// ✨ Some code ommmitted for brevity
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;
};
};