我试图创建一个非矩形元素列表,每个元素都有一个上下文菜单。然而,在进行长按时,我遇到了弯道问题。
使用这种组合的正确方法是什么?我不想使用List,因为它的默认样式。该问题仅在动画制作过程中发生。
longpressed animation process
这是一个简化的代码示例,可以在一个文件中复制、粘贴和运行。
视频是在iOS 18.2的设备上录制的
import SwiftUI
@main
struct MyApp: App {
var body: some Scene { WindowGroup { TestView() } }
}
struct TestView: View {
let items = ["Item 1", "Item 2", "Item 3"]
var body: some View {
VStack {
ForEach(items, id: \.self) { item in
HStack {
Text(item)
Spacer()
Image(systemName: "star")
}
.padding()
.background(.yellow)
// tried all these in different combinations, none works
.contentShape(RoundedRectangle(cornerRadius: 10))
.clipShape(RoundedRectangle(cornerRadius: 10))
.containerShape(RoundedRectangle(cornerRadius: 10))
.contextMenu {
Button { print("Edit \(item)") }
label: { Text("Edit"); Image(systemName: "pencil") }
}
}
}
.padding()
}
}
#Preview { TestView() }