Swift Completion Handler for mapView.setRegion Ani

2019-07-12 19:07发布

问题:

I'm trying to figure out how to tell when the animation is finished for the following:

self.mapView.setRegion(MKCoordinateRegionForMapRect(mapRect), animated: true)

It doesn't look like setRegion supports a completion handler like other commands.

I know how to create a generic completion handler using something like this: How could I create a function with a completion handler in Swift?

But I don't know what variable to check to verify the animation has completed.

回答1:

You can implement the MKMapViewDelegate protocol for this purpose. From the docs:

Because many map operations require the MKMapView class to load data asynchronously, the map view calls these methods to notify your application when specific operations complete.

This method should do what you want:

optional func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool)

Tells the delegate that the region displayed by the map view just changed.