2012-06-29 22 views
6

Mi tarea consiste en anular la selección de una anotación de mapa en el segundo toque.Cómo deseleccionar una anotación de mapa en la segunda pulsación

No encontré cómo hacerlo con las funciones mapView. Por lo que utiliza un artículo de stackoverflow y hacer así:

- (void)viewDidLoad 
{ 
    annotationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationTapRecognized:)]; 
    annotationTap.numberOfTapsRequired = 1; 
    annotationTap.delegate = self; 
} 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    [view addGestureRecognizer:annotationTap]; 
} 

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{ 
    [view removeGestureRecognizer:annotationTap]; 
} 

- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture 
{ 
    NSArray *selectedAnnotations = self.viewMap.selectedAnnotations; 
    for (MapAnnotation *annotationView in selectedAnnotations) { 
     [self.viewMap deselectAnnotation:annotationView animated:NO]; 
    } 
} 

Parece obras correcta, pero no lo es. Cuando toco la anotación, la segunda vez desaparece y aparece de nuevo.

¿Alguna idea?

Gracias de antemano.

Respuesta

18

He encontrado la solución. Tal vez no sea bueno.

He añadido boolean "is show", como se mencionó luxsypher. Así que mis funciones se parecen a las siguientes:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    [view addGestureRecognizer:annotationTap]; 

    if (isShow) { 
     NSArray *selectedAnnotations = self.viewMap.selectedAnnotations; 
     for (MapAnnotation *annotationView in selectedAnnotations) { 
      [self.viewMap deselectAnnotation:annotationView animated:YES]; 
     } 
     isShow = FALSE; 
    } 
} 

- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture 
{ 
    NSArray *selectedAnnotations = self.viewMap.selectedAnnotations; 
    for (MapAnnotation *annotationView in selectedAnnotations) { 
     [self.viewMap deselectAnnotation:annotationView animated:YES]; 
    } 
    isShow = TRUE; 
} 

Tal vez sea útil para alguien :).

Gracias.

1

Quizás deba agregar un booleano "es visible" y actuar en consecuencia. Porque parece que se llama a tu gesto y luego se vuelve a llamar "did Select".

+0

¿Pero dónde he agregado esto? Pensé acerca de esto. Pero, ¿cómo puedo prohibir mostrar texto destacado al hacer clic? Gracias. – Igor

Cuestiones relacionadas