MKAnnotationView and tap detection

前端 未结 6 1307
攒了一身酷
攒了一身酷 2020-12-09 21:29

I have a MKMapView. I added a UITapGestureRecognizer with a single tap.

I now want to add a MKAnnotationView to the map. I can

6条回答
  •  无人及你
    2020-12-09 21:44

    Why not just add UITapGestureRecognazer in viewForAnnotation, use annotation's reuseIdentifier to identify which annotation it is, and in tapGestureRecognizer action method you can access that identifier.

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
        MKAnnotationView *ann = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"some id"];
    
        if (ann) {
            return ann;
        }
    
        ann = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"some id"];
        ann.enabled = YES;
    
        UITapGestureRecognizer *pinTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pinTapped:)];
        [ann addGestureRecognizer:pinTap];
    }
    
    -(IBAction)pinTapped:(UITapGestureRecognizer *)sender {
        MKAnnotationView *pin = (MKPinAnnotationView *)sender.view;
        NSLog(@"Pin with id %@ tapped", pin.reuseIdentifier);
    }
    

提交回复
热议问题