I have a MKMapView
mainMap
in square proportion and its width is equal to the iPhone screen width. I make it circular by using:
mainMap.layer.cornerRadius = mainMap.frame.width/2
mainMap.clipsToBounds = true
Now I have a UIButton
which is underneath the mapView
and it is only partially visible.
This all works great but when I tap the button, the tap is caught by the invisble part of the mapView.
Is there an additional step needed to make the invisible part of mapView not interfere with user interface?
You need to override hit-testing on your background view (hitTest:withEvent:
). You'll call super
, and most of the time you'll return whatever it returns. But if it returns the map view, you'll examine the point (the first parameter) in terms of the map view's frame and decide whether it is inside the circle - this is easy to calculate because CGPath has a CGPathContainsPoint
function. If it is, then return what super
gave you, the map view. But if it is not, then return nil
. The touch will now "fall through" to whatever is behind it, and if that's the button, the button will receive the tap.