0

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.

1
  • You need to provide the complete error message.
    – rmaddy
    May 17, 2015 at 0:43

1 Answer 1

2

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.

2
  • Alex this works! The ONLY gripe i have now is that i have to wait until the view stops changing to have the touch controls enabled. I only want to stop interaction when I'm tapping the cluster. I'll try a few more things to see if i can make that happen, if i can't ill mark your answer as correct and if i can ill still mark it haha. Gimme a few minutes to try this. May 17, 2015 at 1:48
  • It works!!!! I just set the user interaction enabled flag to false before the setRegion function and always enabled it back to true in the regionDidChange function. Thanks a lot Alex! May 17, 2015 at 1:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.