Disable annotation canceled on overlay

On my mapview, I draw polygon overlays that belong to a specific annotation. I want this annotation to be selected when listening to an overlay. My first attempt was to add UITapGestureRecognizerto the mapview, check if the intersection point is inside the polygon and execute [mapView selectAnnotation:myAnnotation]if it was successful. The problem is that after this mapview decides that there is a crane not on any annotations, so it cancels the annotation again.

My question is how best to prevent this; I seem to be unable to find a suitable solution. What I tried:

  • Create a new subclass UIGestureRecognizerthat recognizes only the taps inside the overlays, then iterates through mapView.gestureRecognizersand calls requireGestureRecognizerToFailfor each. However, mapview does not reveal any resolvers through its property.
  • Return YESfor shouldBeRequiredToFailByGestureRecognizerto my custom recognizer for any other recognizer that isKindOfClassrecognizes the icon. However, there seems to be another recognizer that is not passed there.
  • Place a transparent view of it and do a polygon check pointInside:withEvent, but also blocks any other gestures except just taps.

EDIT:

After I looped several times already, I have a code that almost works, about which I know where it goes wrong. I have a custom recognizer, as before. In my business, I do this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
    [otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer]; // can possibly do this in custom recognizer itself instead
    return YES;
}

. , :

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView
{
    // displayRegion is chosen to center annotation
    [mapView setRegion:self.displayRegion animated:YES];
}

, .

+4
1

, ( : )

( , , , ):

:

...
_lp1 = [[UILongPressGestureRecognizer alloc] 
        initWithTarget:self action:@selector(handleOverlayLp1:)];
((UILongPressGestureRecognizer*)_lp1).minimumPressDuration = 0.05;
_lp1.delegate = self;

[_mapView addGestureRecognizer:_lp1];
...

var:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

if (_gestureRecognizers==nil)
    _gestureRecognizers = [NSMutableSet set];
[_gestureRecognizers addObject:otherGestureRecognizer];
return YES;
}

// when i recognize gestures, disable everything and call an asyncrhronous task where i re-enable
- (void)handleOverlayLp1:(UIGestureRecognizer*)recognizer
{

    // Do Your thing. 
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {

        BOOL found=NO;

        ...

        if (found) {
            // disable gestures, this forces them to fail, and then reenable in selectOverlayAnnotation that is called asynchronously
            for (UIGestureRecognizer *otherRecognizer in _gestureRecognizers) {
                otherRecognizer.enabled = NO;
                [self performSelector:@selector(selectOverlayAnnotation:)  withObject:polyline afterDelay:0.1];
            }
        }
    }
}

- (void)selectOverlayAnnotation: (id<MKAnnotation>) polyline
{
    [_mapView selectAnnotation:polyline animated:NO];
    for (UIGestureRecognizer *otherRecognizer in _gestureRecognizers) {
        otherRecognizer.enabled = YES;
    }
}
0

All Articles