更新:为了防止提供有关上下文的错误信息,请阅读我上面添加的答案。
我正在尝试将当前生产就绪的 iOS/iPadOS 17 应用程序调整为visionOS,并且我正在尝试直接运行适用于 Apple Vision Pro(专为 iPad 设计)目标的应用程序。
我有一个底部工作表,可以通过点击地图注释来显示地图项目详细信息。底部工作表可以通过正常的按钮操作正确打开,但不能通过包装按钮操作的注释打开。它在 iOS/iPadOS 上按预期弹出,但在 VisionOS 上却没有。
struct SomeAnnotation: Identifiable, Hashable {
let id: UUID
let title: String?
let coordinate: CLLocationCoordinate2D
let text: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: SomeAnnotation, rhs: SomeAnnotation) -> Bool {
return lhs.id == rhs.id
}
}
struct ContentView: View {
@State var position: MapCameraPosition = .automatic
@State var selected: SomeAnnotation?
@State private var planBottomSheet: Bool = false
let annotations: [SomeAnnotation] = [
SomeAnnotation(
id: UUID(),
title: "Location 1",
coordinate: CLLocationCoordinate2D(
latitude: 43.64191952505181,
longitude: -79.38918661813415
),
text: "Text 1"
),
SomeAnnotation(
id: UUID(),
title: "Location 2",
coordinate: CLLocationCoordinate2D(
latitude: 43.64083254041681,
longitude: -79.3859357809321
),
text: "Text 2"
)
]
var body: some View {
Map(position: $position) {
ForEach(annotations, id: \.self) { result in
Annotation(result.title!, coordinate: result.coordinate) {
Button(action: {
selected = (selected == result ? nil : result)
}) {
Image(systemName: "bolt.car.circle.fill")
.resizable()
.foregroundStyle(.red)
.frame(width: 36, height: 36)
.background(.white)
.clipShape(.circle)
}
}
}
}
.sheet(isPresented: $planBottomSheet) {
SomeView(bottomSheet: $planBottomSheet) // just for an example
}
}
}
struct SomeView: View {
@Binding var bottomSheet: Bool
var body: some View {
VStack {
Text("This is some view")
}
}
}
#Preview {
ContentView()
}
但是,它适用于预期的以下按钮操作,并且可以重现问题并查看以下代码按钮操作,您可以将下面的代码复制/粘贴到身体上方;
var routePlannerButton: some View {
Button(action: {
planBottomSheet = true
if mapViewModel.planDetentCondition {
mapViewModel.planBottomSheetDetent = .large
} else {
mapViewModel.planBottomSheetDetent = .medium
}
}) {
Image(systemName: "point.bottomleft.forward.to.arrowtriangle.uturn.scurvepath.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
.padding()
}
.sheet(isPresented: $planBottomSheet) {
RoutePlannerView(
planBottomSheetDetent: $mapViewModel.planBottomSheetDetent,
planBottomSheet: $planBottomSheet,
isNavigating: $isNavigating,
showingDetails: $showingRouteDetails
)
.presentationDetents(
[mapViewModel.planBottomSheetDetent, .large, .medium],
selection: $mapViewModel.planBottomSheetDetent
)
.presentationContentInteraction(.resizes)
.presentationBackgroundInteraction(.enabled(upThrough: .medium))
.presentationDragIndicator(.visible)
}
}
好吧,问题与 VisionOS SwiftUI iOS 17 地图注释支持无关,但我看到的问题是我没有执行任何操作来触发注释中的 BottomSheet。我的 Map() 还应该跟踪选择。通过选择:在注释上加上 .tag,我根本不需要注释标签中的按钮。另外,我在地图中循环两种不同的类型,而 $selection 正在寻找单一类型。因此充电器的标签将被忽略。