2

I'm trying to add a UIPanGestureRecognizer to my mapView but I don't why the action method is never called (Swift 2.1).

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIGestureRecognizerDelegate
{
    override func viewDidLoad()
    { 
        super.viewDidLoad()

        // other stuff...

        let gestureRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
        gestureRecognizer.delegate = self
        self.mapView.addGestureRecognizer(gestureRecognizer)
    }

    func didDragMap(sender: UIPanGestureRecognizer)
    {
        // never enter here
    }
}

What's wrong here? I have the same Objective-C corresponding and it's work.

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
  • Try printing `mapView.gestureRecognizers`--perhaps there's another pan recognizer already attached? You could try putting a transparent view above your map view and attaching a pan recognizer to that. – nielsbot Mar 22 '16 at 21:12

1 Answers1

3

Since an MKMapView already handles it's own gestures you need to enable it to also listen to your gestures.

Implement shouldRecognizeSimultaneouslyWithGestureRecognizer and return true like so:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

Source

James
  • 1,036
  • 1
  • 8
  • 24