gpt4 book ai didi

ios - 检测 mkoverlay 上的触摸

转载 作者:行者123 更新时间:2023-11-28 10:55:46 26 4
gpt4 key购买 nike

我正在根据方向绘制叠加层。我从路线 a 到路线 b,然后从路线 b 到路线 c。

我想检测覆盖是否在 mkoverlay 上的任何地方被点击。

我用了这个例子Detecting touches on MKOverlay in iOS7 (MKOverlayRenderer)

并将其转换为 swift。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

if let touch = touches.first {
if touch.tapCount == 1 {
let touchLocation = touch.location(in: self)
let locationCoordinate = self.convert(touchLocation, toCoordinateFrom: self)

let mapPoint: MKMapPoint = MKMapPointForCoordinate(locationCoordinate)
let mapPointAsCGP = CGPoint(x: CGFloat(mapPoint.x), y: CGFloat(mapPoint.y))
for overlay: MKOverlay in self.overlays {
if (overlay is MKPolygon) {
let polygon: MKPolygon? = (overlay as? MKPolygon)
let mpr: CGMutablePath = CGMutablePath()
let polygonPoints = polygon?.points
let polygonPointCount = Int((polygon?.pointCount)!)
for p in 0..<polygonPointCount {
let mp: MKMapPoint = polygonPoints.unsafelyUnwrapped()[polygonPointCount]
if p == 0 {
mpr.move(to: CGPoint(x: CGFloat(mp.x), y: CGFloat(mp.y)), transform: .identity)
}
else {
mpr.addLine(to: CGPoint(x: CGFloat(mp.x), y: CGFloat(mp.y)), transform: .identity)
}
}

if mpr.contains(mapPointAsCGP, using: .winding, transform: .identity) {
print("test")
}
}
}

}
}

super.touchesEnded(touches, with: event)
}

我不确定他们为什么在那里有 if 语句

if (overlay is MKPolygon)  

因为它永远不会被调用,因为它是一个 mkoverlay 数组。

最佳答案

我发现这样做比依赖触摸事件方法要好得多。

For your question about:

if (overlay is MKPolygon)

We need to cast the overlay to a polygon so we can get access to the points array, and rebuild the path. Although in my method below, you'll only need access to the points array is the renderer.path is nil. Since I'm using the overlays that are currently on the map, I'm confident that the path is not nil.

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let tap = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))

self.map.addGestureRecognizer(tap)
}

func mapTapped(_ gesture: UITapGestureRecognizer){

let point = gesture.location(in: self.map)

let coordinate = self.map.convert(point, toCoordinateFrom: nil)

let mappoint = MKMapPointForCoordinate(coordinate)

for overlay in self.map.overlays {

if let polygon = overlay as? MKPolygon {

guard let renderer = self.map.renderer(for: polygon) as? MKPolygonRenderer else { continue }

let tapPoint = renderer.point(for: mappoint)

if renderer.path.contains(tapPoint) {

print("Tap was inside this polygon")

break // If you have overlapping overlays then you'll need an array of overlays which the touch is in, so remove this line.
}

continue
}

if let circle = overlay as? MKCircle {

let centerMP = MKMapPointForCoordinate(circle.coordinate)

let distance = MKMetersBetweenMapPoints(mappoint, centerMP) // distance between the touch point and the center of the circle

if distance <= circle.radius {

print("Tap was inside this circle")

break // If you have overlapping overlays then you'll need an array of overlays which the touch is in, so remove this line.
}

continue
}
}
}

关于ios - 检测 mkoverlay 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43778826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com