How can I keep a pin centered on a map whilst I move (via Pan Gesture) another view vertically over the map such that the pin remains above the overlay (not an actual MapKit overlay).
See attached screenshots for the first and final states.
I've got the CGRect of the space between the overlay and the top of the screen as the user pan's up / down. However, how I use that to move the map and pin whilst zooming into the map as the user pans upward..and zoom back out again when the user pans downward, has eluded me so far.
I've tried different approaches, from attempting to adjust the visible rect to adjusting the map view's frame. The answer may lie in some MKMapRect / Region trickery..


(Hand icon by Freepik CC BY 3.0)


To setup the views, place a map view and a
UIViewin your view controller. Use autolayout to pin the map view's left, top and right sides to the superview. Then pin the bottom of the map view to the top of theUIView. Next, pin the left, bottom and right sides of theUIViewto the superview. Finally, set a height constraint on theUIViewto whatever you want it to initialize to. This height value will be changed as the user drags the view. This allows theUIViewto grow as we please and appease autolayout at the same time.Add an
@IBOutletto your view controller for the map view, theUIViewand theUIView's height constraint, like in the code above. TheregionDistanceproperty is what is doing the zooming magic here. It is an exponential equation (that I made up randomly) which will calculate the region for either larger or smaller based on the map view's height.reloadMap()uses this to update the map's zoom. Tying it all together is theUIPanGestureRecognizeron theUIView, which is what controls theUIView's height, causing the zooming action on the map.Pitfall: This forces the map to update the region faster than it can load the region, making it look very jumpy. There is probably ways around this. Get creative.
The coordinate I used in the example is Apple's headquarters.