Attention! The decision made, as well as one of the following, is sometimes erroneous. What for? Sometimes you click on the annotation, but your code will act as if you were using a map. What is the reason for this? Because you listened somewhere around your annotation frame, for example, + 1-6 pixels around, but not within the annotation frame.
It is also interesting that although your code says “you typed a card, not an annotation” in this case, the default code logic on MKMapView will also accept this close tap, as if it were in the annotation area and didSelectAnnotation would fire.
So, you should reflect this problem in your code as well. Let's say this is the default code:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer locationInView:_customMapView]; UIView *v = [_customMapView hitTest:p withEvent:nil]; if (![v isKindOfClass:[MKAnnotationView class]]) { return YES;
And this code also takes into account some proximity affects annotations (because, as said, MKMapView also accepts proximity, not just the right touches):
I turned on Log functions so you can look at it in the console and understand the problem.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer locationInView:_customMapView]; NSLog(@"point %@", NSStringFromCGPoint(p)); UIView *v = [_customMapView hitTest:p withEvent:nil]; if (![v isKindOfClass:[MKAnnotationView class]]) {
My annotation block is 25x25 in size, so I take a distance of 30. You can apply your logic as if (px> = anView.frame.origin.x - 6) && & && Y, etc.
luky
source share