为什么在这个IOS代码中仍然会出现双击?

背景 – 我有一个标注(自定义视图添加到注释视图)的地图(MapKit) – 我已经设法确保在标注视图上做出的“长按”手势没有被拾取(见下面,使用“ shouldReceiveTouch“)

问题我不能做的是同样的事情(即忽略)在标注视图上双击,因为MapKit地图仍然以某种方式挑选并放大。我试着把双击手势放到陷阱它,但它似乎并没有工作。

Mapkit视图委托代码

手势识别器的设置

// Gesture Recogniser (Long Press) let longPressGR = UILongPressGestureRecognizer(target: self, action: "longPressAction:") longPressGR.minimumPressDuration = 1 longPressGR.delegate = self self.mapView.addGestureRecognizer(longPressGR) // Gesture Recogniser (Double Tap) let doubleTapGR = UITapGestureRecognizer(target: self, action: "doubleTapAction:") doubleTapGR.numberOfTapsRequired = 2 doubleTapGR.delegate = self self.mapView.addGestureRecognizer(doubleTapGR) 

为UIGestureRecognizerDelegate实现“shouldReceiveTouch”

 extension GCMapViewHelper : UIGestureRecognizerDelegate { func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { guard let touchesView = touch.view else { fatalError("ERROR: Touch event received was nil") } for annotation in self.mapView.selectedAnnotations { if annotation is GCAnnotation { let annotationView = self.mapView.viewForAnnotation(annotation) if let annotationView = annotationView { if (touchesView.isDescendantOfView(annotationView)) { return false } } } } return true } } 

编辑:这似乎是问题是双击手势识别器不捡双击…任何想法? 即这不工作..

 doubleTapGR.numberOfTapsRequired = 2 

编辑2这里是问题重新编码示例的简化视图:

从下面的代码中我看到在控制台输出的打印语句中:*“longPressAction” – 这个出现/ works *“tapAction” – 这不会出现,即使我双击MKMapView。 所以这是我的问题

 import UIKit import MapKit class ViewController: UIViewController { @IBOutlet weak var mapview: MKMapView! func longPressAction() { print("longPressAction") } func tapAction() { print("tapAction") // ** Not appearing in console ** } override func viewDidLoad() { super.viewDidLoad() // Gesture Recogniser (Long Press) let longPressGR = UILongPressGestureRecognizer(target: self, action: "longPressAction") longPressGR.minimumPressDuration = 1 self.mapview.addGestureRecognizer(longPressGR) let tapGR = UITapGestureRecognizer(target: self, action: "tapAction") tapGR.numberOfTapsRequired = 2 self.mapview.addGestureRecognizer(tapGR) //tapGR.requireGestureRecognizerToFail(longPressGR) } } 

虽然这不是对您的问题的直接答案 – 我有一个解决方法。

如果您将自己的手势识别器添加到自定义注释视图中,则可以同时捕获注释视图和任何相关注释的长按和双击。

我写了一些示例代码来试试这个:

 import UIKit import MapKit class StarAnnotation : NSObject, MKAnnotation { let title: String? let coordinate: CLLocationCoordinate2D init(title: String, coordinate: CLLocationCoordinate2D) { self.title = title self.coordinate = coordinate super.init() } } class StarAnnotationView : MKAnnotationView, UIGestureRecognizerDelegate { override init(annotation: MKAnnotation?, reuseIdentifier: String?) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) self.image = UIImage(named: "star") // Gesture Recogniser (Long Press) let longPressGR = UILongPressGestureRecognizer(target: self, action: "longPressAction:") longPressGR.minimumPressDuration = 1 longPressGR.delegate = self self.addGestureRecognizer(longPressGR) // Gesture Recogniser (Double Tap) let doubleTapGR = UITapGestureRecognizer(target: self, action: "doubleTapAction:") doubleTapGR.numberOfTapsRequired = 2 doubleTapGR.delegate = self self.addGestureRecognizer(doubleTapGR) } override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func doubleTapAction(recognizer: UITapGestureRecognizer) { if recognizer.numberOfTapsRequired == 2 { print("double tapped") } } func longPressAction(recognizer: UILongPressGestureRecognizer) { print("long pressed") } } class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet weak var mapView: MKMapView! override func viewDidLoad() { super.viewDidLoad() // set up map view and sample annotation let operaHouseCoord = CLLocationCoordinate2D(latitude: -33.8587, longitude: 151.2148) let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) let operaHouseAnnotation = StarAnnotation(title: "Opera House", coordinate: operaHouseCoord) mapView.region = MKCoordinateRegion(center: operaHouseCoord, span: span) mapView.delegate = self mapView.addAnnotation(operaHouseAnnotation) } func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if let annotation = annotation as? StarAnnotation { let identifier = "star" if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? StarAnnotationView { dequeuedView.annotation = annotation return dequeuedView } else { let view = StarAnnotationView(annotation: annotation, reuseIdentifier: identifier) view.canShowCallout = true return view } } return nil } }