I have a following setup:
mainViewController
OverlayView - UIView
mapView - MKMapView
My OverlayView is shown over mapView and responds to UIPanGestureRecognizer
Now because OverlayView is above MapView, I can't get the pinch to zoom functionality of MapView to work..
What do I have to do to get mapView to react to pinch and OverLayView to react to Pan (as it does now)?
The solution I have now is just implementing the pinch2zoom functionality in the mainViewController so that it scales mapView accordingly, but it is way less smooth than the original Apple's implementation.
I don't actually now if what I'm going to propose can work or if is a good solution, anyway that is the idea
Set your MainViewController as delegate for the gesture recognizers
If you have a view that is supposed to be in front of the
MKMapView
and not move on the screen as the map moves underneath it, you should implement it as you've described, as a separate view in front of (i.e. on top of) the map view. But rather than having this front view handle gestures and try to change the map view programmatically, you should just set theuserInteractionEnabled
toNO
for this front view (you can do this programmatically or via Interface Builder). This will let the map view behind it receive the touches.If you have some controls on that front view which need to accept user interaction, then go ahead and enable user interaction for those few controls, but make sure that the bulk of this front view is configured to not have
userInteractionEnabled
.If you wanted an overlay that should move with the map, you should just add the overlay to the
MKMapView
, itself, not a separate view. See Displaying Overlays on a Map in the Location Awareness Programming Guide. If you useMKMapView
overlays instead of a separate view, you don't lose any of the built-in gestures.For example, if you set the
delegate
for yourMKMapView
to be your view controller, you can then write arendererForOverlay
in iOS 7 (orviewForOverlay
method for earlier versions):This handles polygons, circles, and lines. Clearly if you're only drawing polygons, for example, you could simplify the above code accordingly.
Once you do that, you can now add an overlay directly to the map. For example, this adds an overlay that is a rectangle of a certain size around a particular coordinate: