gpt4 book ai didi

ios - 在上下文菜单 SwiftUI 中显示不同的 View

转载 作者:行者123 更新时间:2023-12-04 12:34:02 27 4
gpt4 key购买 nike

我正在 SwiftUI 中开发 Mastodon 客户端,我希望允许用户从上下文菜单中查看帖子的预览,就像许多其他应用程序一样。
我知道我可以在 UIKit 中实现这一点,但我还没有在 SwiftUI 中看到如何实现。
例如,假设我有两个 View :CompactView 和 ExpandedView。
CompactView 由 Image 组成和一个 Text行限制为 2,以便隐藏所有其他行。 ExpandedView 也由 Image 组成和一个 Text ,但文本的行数限制为 20,以便显示所有帖子内容。
如果用户强制触摸 CompactView,我想在上下文菜单中显示 ExpandedView 而不是 CompactView。我怎样才能做到这一点?

struct ContentView: some View {

var body: some View {
List(0 ..< 5) {
CompactView()
}
}
}

struct CompactView: some View {

var body: some View {
HStack {
Image("profile")
Text(/*Post content goes here*/)
.lineLimit(2)
}
.contextMenu {
Button("Report", action: {})
}
}
}

struct ExpandedView: some View {

var body: some View {
HStack {
Image("profile")
Text(/*Post content goes here*/)
.lineLimit(20)
}
}
}
我的代码库不是这样,但我猜方法是一样的。
TL;DR:当用户强制触摸 SwiftUI 中的 View 时,我如何显示不同的 View ?
提前致谢。

最佳答案

首先我们需要这个 - Simple way to implement preview context menu for SwiftUI
例如我的内容 View

struct ContentView: View {

let deleteAction = UIAction(
title: "Remove rating",
image: UIImage(systemName: "delete.left"),
identifier: nil,
attributes: UIMenuElement.Attributes.destructive, handler: {_ in print("Foo")})

var body: some View {
NavigationView {
Text("Hello, World!")
.contextMenu(PreviewContextMenu(destination: Text("Destination"), actionProvider: { items in
return UIMenu(title: "My Menu", children: [deleteAction])
}))
}
}}
It's gonna look like that

关于ios - 在上下文菜单 SwiftUI 中显示不同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63131547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com