0

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?

Kashif
  • 4,642
  • 7
  • 44
  • 97

1 Answers1

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Lots of discussion and code examples in my book: http://www.apeth.com/iOSBook/ch18.html#_hit_testing – matt Oct 25 '15 at 15:51
  • Good stuff, I will check it out – Kashif Oct 26 '15 at 16:18
  • anyway to tell if the tap was in the invisible part of the view? – Kashif Oct 28 '15 at 13:52
  • You mean, invisible qua invisible, rather than just knowing where the circle is? Sure, you can just look at the pixel transparency. Again, that's right there in my book (I already gave you the link). But knowing where the circle is seems a lot more obvious to me, in this case at least. – matt Oct 28 '15 at 13:55
  • the reason I am trying to check if the tap was in the invisible part of mapView is that the UIButton (which is underneath the mapview) has a certain part overlapping with mapview and I do not want a tap on that part to be considered a tap on the uibutton. – Kashif Oct 28 '15 at 13:57
  • Then it would be easier just to ask whether the tap point is inside the map view. But it seems I've misunderstood the question entirely; my vision was that the _whole_ button was behind the map view, and that that was the point: the user can see the button thru the map view, and is going to want to be able to tap it. – matt Oct 28 '15 at 13:59
  • Also, isn't this the opposite of what you started out with? You said: "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?" So that is what I answered. Now you say you _do_ want the map view to interfere with the button! Please make up your mind what you want. – matt Oct 28 '15 at 14:00
  • Thanks. i think my question has been collectively answered. I will hit test the tap. If it intersects with uibutton AND intersects with mapview AND mapview pixel transparency is 1.0, it will be true for my purpose. – Kashif Oct 28 '15 at 14:07