我正在尝试向ContentView.swift中的Mapkit地图视图添加手势(轻击,轻扫等),但无济于事。我从googlemaps开始,然后尝试了Mapkit。我知道有两种向视图添加手势的方法。
import SwiftUI
struct ContentView: View {
var body: some View {
MapView()
.onTapGesture(count: 2) {
print("Double tapped!")
}
.edgesIgnoringSafeArea(.all)
}
struct MapView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator
let tap = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.doubleTapped))
tap.numberOfTapsRequired = 2
mapView.addGestureRecognizer(tap)
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
@objc func doubleTapped() {
print("Double Tapped...")
}
}
}
这些方法似乎都不起作用。对我来说,更可取的方式是#1,但是我尝试了像#2一样的替代方法。关于我在做什么错的任何想法吗?我正在使用Xcode 11.4(11E146)。谢谢!