VisionOS为何不支持MapKit Annotation点击交互?适配iPad应用遇异常
我正在把一款已上线的iOS/iPadOS 17应用适配到VisionOS,以「专为iPad设计」的目标在Apple Vision Pro上测试。当前遇到的问题是:点击Map Annotation包裹的Button时,无法触发底部弹窗的唤起操作,但普通Button可以正常唤起该弹窗,且这个功能在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) // 示例视图 } } } struct SomeView: View { @Binding var bottomSheet: Bool var body: some View { VStack { Text("这是示例视图") } } } #Preview { ContentView() }
以下是可正常唤起弹窗的普通Button代码,可复制到上述ContentView的body中用于对比测试:
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) } }
内容的提问来源于stack exchange,提问作者Emre Değirmenci
相关产品推荐
相关产品推荐

