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:
- (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 the MKMapView
(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?
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 that MKAnnotationView
(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 appropriate UIView
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With