我正在尝试添加点击识别器以显示其他信息标注。我尝试直接调用选择器“ showPersonInfo”,它正在工作。但是,当我尝试在MKAnnotationView的子视图上的UITapGestureRecognizer中添加它时,我正在研究。当我点击时,选择器没有启动。

这段代码在MKAnnotationView的子类的.m内部

- (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];

}

最佳答案

您可以使用mapView Delegate方法将操作添加到注释视图

 - (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;
    }

10-06 10:50