简体   繁体   English

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

[英]UITapGestureRecognizer on a subview of MKAnnotationView not working

I am trying to add tap recognizer to show additional information callout. 我正在尝试添加点击识别器以显示其他信息标注。 I tried calling the selector "showPersonInfo" directly and it's working. 我尝试直接调用选择器“ showPersonInfo”,它正在工作。 But, when I try to add it in a UITapGestureRecognizer on a subview of the MKAnnotationView I am working on. 但是,当我尝试在MKAnnotationView的子视图上的UITapGestureRecognizer中添加它时,我正在研究。 The selector is not firing when I tap. 当我点击时,选择器没有启动。

This code is inside .m of a subclass of MKAnnotationView 这段代码在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];

}

You can use mapView Delegate method for adding actions to your annotation view 您可以使用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;
    }

The two things you need to aware of : 您需要注意的两件事:

By default the UIIMageView.userInteraction is disabled 默认情况下, UIIMageView.userInteractiondisabled

   self.imageContainerView.userinteractionenabled = yes;

UITapGesture: UITapGesture:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM