[英]SwiftUI Views blocking touches to MapKit but not other views
我正在通过 UIViewRepresentable 呈现 mapView,并希望在 map 的中心有一个图像“目标”。 此图像会阻止在图像上开始的点击事件和平移手势。 因此,将注释放到图像下方的 map 上意味着无法启动其标注。
我已经阅读了有关透明度问题的信息,因此我将具有“透明”区域的目标图像重新制作为不透明度为 1% 但行为没有变化的白色。
将allowsHitTesting
设置为false也不起作用,尽管如果我在图像下方插入一个按钮(代码中的测试1)我可以点击它,而不是地图/注释,所以MapKit元素和SwiftUI视图之间似乎存在差异我自己加。
import SwiftUI
import MapKit
struct ContentView: View {
var body: some View {
ZStack {
MapView()
/*Test 1*/ Button("Tap Me") {
print("Text button was tapped")
}
Image("target")
.frame(width: 90, height: 90)
.allowsHitTesting(false)
/*Test 2*/ Rectangle()
.fill(Color.red.opacity(0.2))
.frame(width: 90, height: 90)
.allowsHitTesting(false)
/*Test 3*/ Button(action: {print("Round button was tapped")}) {
Circle()
.fill(Color.red.opacity(0.2))
.frame(width: 90, height: 90)
}
.allowsHitTesting(false)
.disabled(true)
}
}
}
struct MapView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.mapType = .standard
map.isRotateEnabled = false
let location = CLLocation(latitude: 40.763783, longitude: -73.973133)
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 2000, longitudinalMeters: 2000)
map.setRegion(region, animated: true)
let annotation = MyAnnotation(coordinate: location.coordinate)
annotation.title = "Title"
annotation.subtitle = "Subtitle"
map.addAnnotation(annotation)
return map
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
class MyAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
将图像交换为矩形(代码中的测试 2)具有相同的行为,我可以与我添加的视图交互,但不能将我的触摸/平移传递给 MapKit 元素。 我什至尝试使用禁用的按钮,希望它的命中测试更符合我想要实现的目标。 可悲的是没有。
有谁知道通过另一个视图与 mapView、点击和平移交互的方法?
是的,到目前为止,即使是透明图像也不允许穿透。 在您的情况下,很简单,可能的方法是创建自定义形状,如下所示。 形状确实通过命中。
这是显示方向的简单形状演示。
struct Cross: Shape {
func path(in rect: CGRect) -> Path {
return Path { path in
path.move(to: CGPoint(x: rect.midX, y: 0))
path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
path.move(to: CGPoint(x: 0, y: rect.midY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
path.move(to: CGPoint(x: rect.midX, y: rect.midY))
path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: 10, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 360), clockwise: false)
}
}
}
struct ContentView: View {
var body: some View {
ZStack {
MapView()
Cross().stroke(Color.red)
.frame(width: 90, height: 90)
}
}
}
如果您禁用以上所有功能...
所以按钮“点击我”有效。
struct ContentView: View {
var body: some View {
ZStack {
MapView()
/*Test 1*/ Button("Tap Me") {
print("Text button was tapped")
}
Image("target")
.frame(width: 90, height: 90)
.allowsHitTesting(false)
.allowsHitTesting(false)
.disabled(true)
/*Test 2*/ Rectangle()
.fill(Color.red.opacity(0.2))
.frame(width: 90, height: 90)
.allowsHitTesting(false)
.allowsHitTesting(false)
.disabled(true)
/*Test 3*/ Button(action: {print("Round button was tapped")}) {
Circle()
.fill(Color.red.opacity(0.2))
.frame(width: 90, height: 90)
}
.allowsHitTesting(false)
.disabled(true)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.