在Uicontextmenu预览提供商中接受TAPS

发布于 2025-01-21 17:27:00 字数 1077 浏览 3 评论 0原文

tl; dr:如何在uicontextmenucontentpreviewprovider查看控制器中接受按钮上的龙头上的龙头?

我正在为uicontextmenu介绍一个自定义预览控制器,通过uicontextmenucontentpreviewprovider在我的uicontextmenuconfiguration initializer中设置。该视图按按钮按预期显示,但是点击它只是将预览丢弃,而没有按钮捕获的触摸。这是预览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)
    }
)

tl;dr: How do I accept taps on a button within a UIContextMenuContentPreviewProvider view controller?

I'm presenting a custom preview controller for a UIContextMenu, set via UIContextMenuContentPreviewProvider in my UIContextMenuConfiguration initializer. The view displays as expected with a button, but tapping it simply dismisses the preview, with no touches being captured by the button. Is that an inherent limitation of the preview API, or is there something I'm missing to enable interaction?

Here's a rough example of the code creating the preview:

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)
    }
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2025-01-28 17:27:00

您只能在整个预览视图上使用点击:

查看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
    }
}

You can handle only tap on whole preview view:

Take a look at UIContextMenuInteractionDelegate

func contextMenuInteraction(
    _ interaction: UIContextMenuInteraction,
    willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration,
    animator: UIContextMenuInteractionCommitAnimating
) {
    animation.onCompletion {
        // your code
    }
}

Or for WKWebView use WKUIDelegate

func webView(
    _ webView: WKWebView,
    contextMenuForElement elementInfo: WKContextMenuElementInfo,
    willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating
) {
    animation.onCompletion {
        // your code
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文