首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测mkoverlay上的触点

检测mkoverlay上的触点
EN

Stack Overflow用户
提问于 2017-05-04 09:21:00
回答 1查看 1.6K关注 0票数 1

我是根据方向画覆盖图的。我从路线a画到b路,再画b路到c路。

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

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

把它改装成了斯威夫特。

代码语言:javascript
复制
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声明

代码语言:javascript
复制
if (overlay is MKPolygon)  

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

EN

回答 1

Stack Overflow用户

发布于 2017-05-04 23:59:37

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

你的问题是: 如果(覆盖是MKPolygon) 我们需要将覆盖转换成一个多边形,这样我们就可以访问点数组,并重新构建路径。尽管在下面的方法中,您只需要访问points就是renderer.path为零。因为我使用的是当前地图上的覆盖,所以我确信路径不是零。

代码语言:javascript
复制
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
        }
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43778826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档