Note. This answer became possible and was adapted from Yano's answer to the same question for Objective-C here: determine if MKMapView was moved / moved . Thanks Jano .
To correctly detect the dragging of a map, you must add a UIPanGestureRecognizer. This is a sign of gesture recognition (pan = drag).
Step 1: Add a gesture recognizer to viewDidLoad (Swift 2)
override func viewDidLoad() { super.viewDidLoad()
Swift 3 version higher than gesture recognition setting (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 UIGestureRecognizerDelegate protocol to the view controller so that it works as a delegate.
class MapViewController: UIViewController, UIGestureRecognizerDelegate
Step 3: Add the following code for UIPanGestureRecognizer to work with 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 of 50 times for a drag, define in your village the state of "drag" or "drag":
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!
source share