Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if MKMapView was dragged/moved in Swift 2.0

How do I detect when the user has dragged or move an MKMapView in order to, e.g., prevent automatic homing in on the user's current location.

like image 826
basicallydan Avatar asked Oct 29 '15 03:10

basicallydan


1 Answers

Note: This answer was made possible and was adapted from Jano's response to the same question for Objective-C here: determine if MKMapView was dragged/moved. Thanks Jano.

To properly detect a map drag you have to add a UIPanGestureRecognizer. This is the drag gesture recognizer (panning = dragging).

Step 1: Add the gesture recognizer in viewDidLoad (Swift 2)

override func viewDidLoad() {
    super.viewDidLoad()
    // All your other setup code
    let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
    mapDragRecognizer.delegate = self
    self.mapView.addGestureRecognizer(mapDragRecognizer)
}

Swift 3 version of above gesture recognizer setup (selector syntax has changed)

override func viewDidLoad() {
    super.viewDidLoad()
    let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.didDragMap(gestureRecognizer:)))
    mapDragRecognizer.delegate = self
    self.mapView.addGestureRecognizer(mapDragRecognizer)
}



Step 2: Add the protocol UIGestureRecognizerDelegate to the view controller so it works as delegate.

class MapViewController: UIViewController, UIGestureRecognizerDelegate

Step 3: Add the following code for the UIPanGestureRecognizer to work with the already existing gesture recognizers in MKMapView:

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

Step 4: In case you want to call your method once instead 50 times per drag, detect either the "drag ended" or "drag began" state in your selector:

func didDragMap(gestureRecognizer: UIGestureRecognizer) {
    if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
        print("Map drag began")
    }

    if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {
        print("Map drag ended")
    }
}

Hope this helps someone in need!

like image 111
basicallydan Avatar answered Oct 16 '22 17:10

basicallydan