Reputation: 1025
I've had this problem for months and I'm asking for help now. I've tried a slew of things. My problem is that in my iOS 8 Swift application I am using map annotation clustering which works fine, and when a user taps on a clustered pin on the map the mapView zooms in to the center of that region which de-clusters the annotation view into individual views (depending on the number of annotations) using the function mapView.setRegion(region: myRegion, animated: true)
. This works just fine and everything about it is lovely.
My issue (which I discovered by accident) was that while I was calling the setRegion
function, and the map starts to zoom in animated, if I try to scroll to a different position on the map while the animation is still happening I get a crash with the following output in the console:
Terminating app due to uncaught exception 'NSInvalidArgumentException' unrecognized selector sent to instance 0x126d0a890'
So with this in mind this crash doesn't happen when I set the animation flag to false
in setRegion()
. But what I want to do is disable touch events while the view is animating which I thought happened automatically when setRegion
gets called and it sets userInteractionEnabled
by default to false.
I have tried this code with no success
var center = view.annotation.coordinate
var span = MKCoordinateSpanMake(mapView.region.span.latitudeDelta/5.0, mapView.region.span.longitudeDelta/5.0)
mapView.userInteractionEnabled = false
mapView.scrollEnabled = false
mapView.rotateEnabled = false
mapView.setRegion(MKCoordinateRegionMake(center, span), animated: true)
mapView.userInteractionEnabled = true
mapView.scrollEnabled = true
mapView.rotateEnabled = true
I'm not sure what to do anymore as I want to keep the animation. Any help would be appreciated people.
Upvotes: 0
Views: 1207
Reputation: 1001
The setRegion initiates the animated update, which will not complete, and most likely will not even start after executing the setRegion line in your code. It happens asynchronously.
That means disabling and and re-enabling user interactions has no effect.
In your controller you need to handle the following MKMapViewDelegate callbacks:
mapView(_:regionWillChangeAnimated:)
mapView(_:regionDidChangeAnimated:)
Disable user interaction and/or map updates before animation starts and enable when it is done.
Upvotes: 2