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];
}
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;
}
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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.