Nik Kov Nik Kov - 5 months ago 17
iOS Question

Double tap on annotation view

Ok ppl, there the situation:

I have annotation markers and when i tap on one of them, it calls the method

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MYCustomAnnotationView *)view


Then i take some property's values from (MYCustomAnnotationView *)view and, depend on them, do some movements.

I need to do this movements only when annotation is double tapped.

What did i do:


  1. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapOnAnnotationView)];

    Failed because i need to send
    (MYCustomAnnotationView *)view
    . I tried to store last hitted annotation, but it not seems to be right way.

  2. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] performSelector:@selector(doubleTapOnAnnotationView:mapView:) withObject:view withObject:mapView];

    but it not works.

  3. tapCount++;
    switch (tapCount)
    {
    case 1: //single tap
    [self performSelector:@selector(singleTap:) withObject: nil afterDelay: 0.2];
    break;
    case 2: //double tap
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap:) object:nil];
    [self performSelector:@selector(doubleTap:) withObject: nil];
    break;
    default:
    break;
    }
    if (tapCount>2) tapCount=0;

    but it not works with this method properly.



I think i need something like completionHandler, when double tap. Maybe somebody knows what can help in this situation?

Answer

1) In -mapView viewForAnnotation

if ([annotationView gestureRecognizers] == nil) {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapOnAnnotationView:)];
        tapGesture.numberOfTapsRequired = 2;
        [annotationView addGestureRecognizer:tapGesture];
    }

2)

-(void)doubleTapOnAnnotationView:(UITapGestureRecognizer *)tap
{
MYCustomAnnotationView *view = (MYCustomAnnotationView*)tap.view;
...
}

Thanks to Larme.