0
votes

I am trying to add tap recognizer to show additional information callout. I tried calling the selector "showPersonInfo" directly and it's working. But, when I try to add it in a UITapGestureRecognizer on a subview of the MKAnnotationView I am working on. The selector is not firing when I tap.

This code is inside .m of a subclass of MKAnnotationView

- (void)layoutSubviews {

    [self addSubView:self.imageContainerView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPersonInfo:)];
    [self.imageContainerView addGestureRecognizer:tap];

}

- (void)showPersonInfo:(UITapGestureRecognizer *)tap {

    NSLog(@"annotation imageView touched");
    [self addSubview:self.personInfoView];

}
2

2 Answers

1
votes

You can use mapView Delegate method for adding actions to your annotation view

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if (annotation == mapView.userLocation)
            return nil;

        static NSString *s = @"identifier";
        MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
        if (!pin) {
            pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
            pin.canShowCallout = YES;
            pin.image = [UIImage imageNamed:@"pin.png"];
            pin.calloutOffset = CGPointMake(0, 0);
            UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [button addTarget:self
                       action:@selector(showPersonInfo:) forControlEvents:UIControlEventTouchUpInside];
            pin.rightCalloutAccessoryView = button;
        }
        return pin;
    }
0
votes

The two things you need to aware of :

By default the UIIMageView.userInteraction is disabled

   self.imageContainerView.userinteractionenabled = yes;

UITapGesture:

[tapRecognizer setDelegate:self];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];