防止手势通过注释视图到达地图
Preventing Gestures From Going Through An Annotation View to the Map
我有一个要插入到 MKAnnotationView 中的 UIView。
它有一个以编程方式生成的手势识别器。
问题是偶尔点击注释,也会(或相反)进入地图。
我附上了演示,归结为一个丑陋、简单的 ViewController(请查看屏幕截图以了解它的外观)。
如果您使用它创建应用程序,然后在底部方块上重复 click/tap(它们会在 clicks/taps 上改变颜色),地图将放大。
可能是它把一些点击解释为双击,但我不能确定(模拟器地图是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
}
}
}
只是想提一下我是如何"solved"这个的。
像许多此类事情一样,我必须想出一种完全不同的方法来做到这一点,而不是试图把鞋拔到黑客的地步。
我处理它的方法是创建一个透明的 UIView,我将其放置在 MapView 之上的视图层次结构中。然后我将我的交互元素添加到这个视图中,并在完成后刷新所有内容。
当您点击其他地方时,它有点奇怪,但与平移或缩放地图时所造成的损害相比,没有任何差别。我想我可以稍微想一想就能解决这个问题。
我有一个要插入到 MKAnnotationView 中的 UIView。
它有一个以编程方式生成的手势识别器。
问题是偶尔点击注释,也会(或相反)进入地图。
我附上了演示,归结为一个丑陋、简单的 ViewController(请查看屏幕截图以了解它的外观)。
如果您使用它创建应用程序,然后在底部方块上重复 click/tap(它们会在 clicks/taps 上改变颜色),地图将放大。
可能是它把一些点击解释为双击,但我不能确定(模拟器地图是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
}
}
}
只是想提一下我是如何"solved"这个的。
像许多此类事情一样,我必须想出一种完全不同的方法来做到这一点,而不是试图把鞋拔到黑客的地步。
我处理它的方法是创建一个透明的 UIView,我将其放置在 MapView 之上的视图层次结构中。然后我将我的交互元素添加到这个视图中,并在完成后刷新所有内容。
当您点击其他地方时,它有点奇怪,但与平移或缩放地图时所造成的损害相比,没有任何差别。我想我可以稍微想一想就能解决这个问题。