如何在 SwiftUI 的新地图视图中制作可点击的图钉?

Ber*_*nga 5 swiftui ios14

我正在使用 SwiftUI 的新地图视图来显示注释的大头针,并希望大头针在单击时显示用于编辑注释名称和描述的视图。

我尝试将 MapAnnotation 与包含带有图钉图像的按钮的视图一起使用。图像显示正确,但按钮不起作用。

是否可以在不回退到 MKMapView 的 UIViewRepresentable 的情况下执行此操作?

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var showingEditScreen = false

    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 37.334722,
                                       longitude: -122.008889),
        span: MKCoordinateSpan(latitudeDelta: 1,
                               longitudeDelta: 1)
    )

    @State private var pins: [Pin] = [
        Pin(name: "Apple Park",
            description: "Apple Inc. headquarters",
            coordinate: CLLocationCoordinate2D(latitude: 37.334722,
                                               longitude:-122.008889))
    ]

    var body: some View {
        Map(coordinateRegion: $region,
            interactionModes: .all,
            annotationItems: pins,
            annotationContent: { pin in
                MapAnnotation(coordinate: pin.coordinate,
                              content: {
                                PinButtonView(pin: pin)
                              })
            })
    }
}
Run Code Online (Sandbox Code Playgroud)

我的引脚定义:

struct Pin: Identifiable {
    let id = UUID()
    var name: String
    var description: String
    var coordinate: CLLocationCoordinate2D

}
Run Code Online (Sandbox Code Playgroud)

带有按钮和图钉图像的视图:

struct PinButtonView: View {
    @State private var showingEditScreen = false
    @State var pin: Pin

    var body: some View {
        Button(action: {
            showingEditScreen.toggle()
        }) {
            Image(systemName: "mappin")
                .padding()
                .foregroundColor(.red)
                .font(.title)
        }
        .sheet(isPresented: $showingEditScreen,
               content: {
                EditView(pin: self.$pin)
               })
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑视图:

struct EditView: View {
    @Environment(\.presentationMode) var presentationMode

    @Binding var pin: Pin

    var body: some View {
        NavigationView {
            Form {
                    TextField("Place name", text: $pin.name)

                    TextField("Description", text: $pin.description)
            }
            .navigationTitle("Edit place")
            .navigationBarItems(trailing: Button("Done") {
                self.presentationMode.wrappedValue.dismiss()
            })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

bit*_*wit 3

从 XCode 12.3 开始,这似乎可以通过按钮来运行。我注意到的一个关键问题是,如果您使用偏移量,那么您的按钮可能会被放置在注释的可点击区域之外。

为了解决这个问题,您可以添加额外的填充来解决偏移并将其保持在可点击的范围内:

MapAnnotation(coordinate: item.placemark.coordinate) {
  ZStack {
      MapPinView() // My view for showing a precise pin
      VStack { // A prompt with interactive option, offset by 60pt
          Text("Use this location?")
          HStack {
              Button("Yes", action: {
                  print("yes")
              })
              
              Button("No", action: {
                  print("no")
              })
          }
      }
      .offset(x: 0, y: 60) // offset prompt so it's below the marker pin
  }
  .padding(.vertical, 60) // compensate for offset in tappable area of annotation. pad both the top AND the bottom to keep contents centered
}
Run Code Online (Sandbox Code Playgroud)