tl;dr: 如何接受
UIContextMenuContentPreviewProvider
视图控制器中按钮的点击?
我正在为
UIContextMenu
提供一个自定义预览控制器,通过我的 UIContextMenuContentPreviewProvider
初始化程序中的 UIContextMenuConfiguration
设置。视图按预期显示,带有按钮,但点击它只会取消预览,按钮不会捕获任何触摸。这是预览 API 的固有限制,还是我缺少某些东西来启用交互?
这是创建预览的代码的粗略示例:
return UIContextMenuConfiguration(
identifier: messageId.value as NSString,
previewProvider: nil,
previewProvider: {
let viewController = UIViewController()
viewController.view.backgroundColor = .green
let button = UIButton(type: .infoLight)
button.addAction(UIAction(handler: { _ in
print("Button")
}), for: .touchUpInside)
button.sizeToFit()
viewController.view.addSubview(button)
button.center = viewController.view.center
return viewController
},
actionProvider: {_ in
return UIMenu(title: "", children: reactionOptions)
}
)
您只能处理整个预览视图上的点击:
看看
UIContextMenuInteractionDelegate
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration,
animator: UIContextMenuInteractionCommitAnimating
) {
animation.onCompletion {
// your code
}
}
或者对于
WKWebView
使用 WKUIDelegate
func webView(
_ webView: WKWebView,
contextMenuForElement elementInfo: WKContextMenuElementInfo,
willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating
) {
animation.onCompletion {
// your code
}
}