swift - 防止手势通过注释 View 到达 map

标签 swift uiview mapkit uigesturerecognizer mkannotation

我有一个 UIView,我将其插入到 MKAnnotationView 中。

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

问题是,有时,点击注释,也会(或相反)进入 map 。

我附上了演示,它被归结为一个丑陋的、简单的 ViewController(请参阅屏幕截图以了解它的外观)。

如果您用它创建一个应用程序,然后反复单击/点击底部的方 block (它们会在点击/点击时改变颜色), map 将会放大。

它可能会将某些点击解释为双击,但我不能确定(模拟器 map 很慢)。

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

隐藏你的眼睛。这会很糟糕:

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

最佳答案

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

就像许多此类事情一样,我必须找出一种完全不同的方法来做到这一点,而不是试图硬塞到黑客攻击的地步。

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

当您单击其他地方时,它有点奇怪,但与 map 平移或缩放时造成的损坏相比,没有什么可比的。我怀疑我能够通过一些思考来解决这个奇怪的问题。

关于swift - 防止手势通过注释 View 到达 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52798440/

相关文章:

ios - 尝试通过快速桥接头调用扩展方法时发送到实例的无法识别的选择器

swift - 使用 Storyboard 中的 View 将其框架用于以编程方式设置的另一个 View

ios - 它不会构建失败,它会显示 "No Issues"但仍然无法正常工作

objective-c - 添加新引脚后更改区域

swift - 在for循环中附加图像url数组,然后发布到数据库

objective-c - 在 Swift 类的指定初始化器中调用实例方法

ios - Xcode 5 项目中没有 Swift 设置

iOS 动画故障,Y 坐标问题

ios - 防止点击覆盖时取消选择注释

ios - MKMapSnapshotter 从快照中删除 "Legal"