MKAnnotationView子视图上的UITapGestureRecognizer无法正常工作

时间:2014-06-26 08:54:51

标签: ios objective-c mkannotationview uitapgesturerecognizer

我正在尝试添加点击识别器以显示其他信息标注。我试着调用选择器" 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];

}

2 个答案:

答案 0 :(得分:1)

您可以使用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;
    }

答案 1 :(得分:0)

您需要注意的两件事:

默认情况下,UIIMageView.userInteractiondisabled

   self.imageContainerView.userinteractionenabled = yes;

<强> UITapGesture:

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