簡體   English   中英

MKAnnotationView的子視圖上的UITapGestureRecognizer無法正常工作

[英]UITapGestureRecognizer on a subview of MKAnnotationView not working

我正在嘗試添加點擊識別器以顯示其他信息標注。 我嘗試直接調用選擇器“ 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;
    }

您需要注意的兩件事:

默認情況下, UIIMageView.userInteractiondisabled

   self.imageContainerView.userinteractionenabled = yes;

UITapGesture:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM