防止手势通过注释视图进入地图

时间:2018-10-14 00:17:33

标签: swift uiview mapkit uigesturerecognizer mkannotation

我有一个插入到MKAnnotationView中的UIView。

它具有以编程方式生成的手势识别器。

问题在于,有时会进入注释,也(或改为)进入地图。

我随函附上了这个演示,该演示归结为一个丑陋,简单的ViewController(请参阅屏幕截图以查看其外观)。

如果您使用该应用创建了一个应用,然后在底部的正方形上反复单击/轻击(它们会在单击/轻击时改变颜色),地图将会放大。

可能是它将某些水龙头解释为两次轻敲,但我不确定(模拟器映射为S L O W)。

防止注释下的手势识别器获取注释中的事件(甚至两次轻击)的最佳方法是什么?

隐藏你的眼睛。这将是丑陋的:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let washingtonMonument = CLLocationCoordinate2D(latitude: 38.8895, longitude: -77.0353)
        let annotation = GestureAnnotation()
        annotation.coordinate = washingtonMonument
        self.mapView.addAnnotation(annotation)
        let washingtonRegion = MKCoordinateRegion(center: washingtonMonument, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
        self.mapView.setRegion(washingtonRegion, animated: false)
    }

    func mapView(_ inMapView: MKMapView, viewFor inAnnotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = inAnnotation as? GestureAnnotation {
            return annotation.viewObject
        }
        return nil
    }
}

class GestureTargetView: UIView {
    let colors = [UIColor.red, UIColor.yellow, UIColor.black, UIColor.green]
    var tapGestureRecognizer: UITapGestureRecognizer?
    var currentColorIndex = 0

    override func layoutSubviews() {
        if nil == self.tapGestureRecognizer {
            self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(type(of: self).handleTap))
            self.addGestureRecognizer(self.tapGestureRecognizer!)
        }
        self.backgroundColor = self.colors[0]
    }

    @objc func handleTap(sender: UITapGestureRecognizer) {
        if .ended == sender.state {
            self.currentColorIndex += 1
            if self.currentColorIndex == self.colors.count {
                self.currentColorIndex = 0
            }
            self.backgroundColor = self.colors[self.currentColorIndex]
        }
    }
}

class GestureAnnotationView: MKAnnotationView {
    var gestureView: GestureTargetView!

    override func prepareForDisplay() {
        self.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: 128, height: 128))
        if nil == self.gestureView {
            self.gestureView = GestureTargetView(frame: self.frame)
            self.addSubview(self.gestureView)
        }
        super.prepareForDisplay()
    }
}

class GestureAnnotation: NSObject, MKAnnotation {
    var myView: GestureAnnotationView!
    var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
    var viewObject: MKAnnotationView! {
        get {
            if nil == self.myView {
                self.myView = GestureAnnotationView(annotation: self, reuseIdentifier: "")
            }

            return self.myView
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

只想提一下我是如何“解决”这个问题的。

与许多这类事情一样,我不得不想出一种完全不同的方式来实现这一目标,而不是试图将鞋拔到黑客的地步。

我处理的方式是创建一个透明的UIView,该UIView放置在MapView的视图层次结构中。然后,我将交互式元素添加到此视图中,并在完成后刷新所有内容。

当您在其他位置单击时,它有些奇怪,但是与平移或缩放地图所造成的损害甚至相去甚远。我怀疑我可以稍微思考一下就能解决这个奇怪问题。