I have a custom annotation view on the map, which has a UIButton
in it, but the UIButton
is not responsive when pressed. I have two main problems with user interaction on the annotation view:
- Buttons and other controls are not responsive.
- I want the annotation to block touches according to my implementation of
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
- that is if I return YES then I don't want the touches to get sent through to theMKMapView
(potentially selecting other annotations that are BEHIND my annotation view), I want to handle the touch myself in this case.
I have made sure userInteractionEnabled
is set to YES
and I have investigated how touches are sent to the custom annotation view (my subclass of MKAnnotationView
) by overriding touchesBegan
etc. - but it appears that the touches are usually cancelled (thought I've managed to get touchesEnded
a few times) - so it seems like it will even be difficult to manually implement any user-interaction with the custom annotation view.
Does anyone have any insights into allowing more user interaction with MKAnnotationView
objects?
Adding up to the answer of jhabbott, this is what worked for me. I have a custom annotation view
MKCustomAnnotationView
that holds a custom annotationCustomPin
as annotation. That 'pin' holds aUIButton
as accessory button replacement which I wanted to get touch events.My
hitTest
method would look like this:The
calloutActive
bool is probably not necessary in most cases.I managed to resolve this with the help of a colleague. The solution is to override
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
. Our assumption is thatMKAnnotationView
(which your annotation view must inherit from) overrides this to do the 'wrong' thing (presumably so that annotation selection doesn't get blocked between overlapping annotations). So you have to re-override it to do the right thing and return the appropriateUIView
, the system will then send the events to it and the user will be able to interact with it :). This has the beneficial (in this case) side-effect that the interactive annotation blocks the selection of annotations that are behind it.For anyone looking to add a tapGesture to an AnnotationView subview then the answer at the bottom of this:
MKannotationView with UIButton as subview, button don't respond
Worked for me:
I found that rather than overriding
hitTest:withEvent:
I could just overridepointInside:withEvent:
instead and just get it to returnYES
. I guess that officially I should be doing a point-rect intersect check to ensure the place I'm tapping is within the control element, but in practise, just puttingreturn YES
appears to work perfectly well, still allowing you to dismiss the MKAnnotationView by tapping away from it.