自定义MKMapView标注不响应触摸事件

dan*_*n78 2 objective-c uiview mapkit ios

我有一个自定义UIView,当用户点击一个自定义注释时,我将其显示为标注MKMapView.

为了达到这个目的,我已经按照本答案中的建议MKAnnotationView对子-setSelected:selected animated:方法进行了子类化和重载.基本上,我将我的自定义视图添加为我的子类的子视图.MKAnnotationView

问题是我无法与标注进行交互,标注包含一个按钮和一个可滚动的webview.更重要的是,如果标注隐藏了注释并且我在该隐藏注释的大致位置按下标注,则标注将被解除并且将显示新标注.

// TPMapAnnotationView.m
@implementation TPMapAnnotationView
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    if(selected)
    {
        TPMapAnnotation* anno = ((TPMapAnnotation*)self.annotation);
        QuickInfoView* qi = [[QuickTabView alloc] initWithFrame:CGRectMake(0, 0, 440, 300)];
        [qi displayDataForAnnotation:anno];
        [self addSubview:qi];
        // some animiation code that doesn't change things either way
    }
    else
    {
        [[self.subviews objectAtIndex:0] removeFromSuperview];
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的代码创建了TPMapAnnotationView.

// this is in the view controller that contains the MKMapView
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id) annotation 
{  
    if ([annotation isKindOfClass:[TPMapAnnotation class]])
    {
        TPMapAnnotationView *customAnnotationView = (TPMapAnnotationView *)[myMap dequeueReusableAnnotationViewWithIdentifier:@"TPAnn"];
        if (customAnnotationView == nil)
        {
            customAnnotationView = [[TPMapAnnotationView alloc] initWithAnnotation:annotation 
                                                                reuseIdentifier:@"TPAnn"];
        }
        [customAnnotationView setImage:annotationImage];
        return customAnnotationView;
    }
    return nil; // blue radar circle for MKUserLocation class.
}
Run Code Online (Sandbox Code Playgroud)

cha*_*tur 6

这是一个众所周知的问题.您在AnnotationView上添加的任何内容都不会检测到触摸.这个问题有很好的开源项目.http://dev.tuyennguyen.ca/wp-content/uploads/2011/03/CustomMapAnnotationBlogPart1.zip,http://dev.tuyennguyen.ca/wp-content/uploads/2011/03/CustomMapAnnotationBlogPart21.zip

EDIT:
Run Code Online (Sandbox Code Playgroud)

是.我也努力将uibuttons添加到我自己的自定义annotationView中,但后来我偶然发现了这个项目并发现他的自定义annotationView实际上是一个注释.

无论如何,如果你想改变annotatioView的高度,那么你可以设置

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

 calloutMapAnnotationView.contentHeight = height;
 calloutMapAnnotationView.titleHeight = 25;
}
Run Code Online (Sandbox Code Playgroud)

这里,titleHeight是添加到CalloutMapAnnotationView的属性,它决定了drawRectMethod中"gloss"的高度

- (void)drawRect:(CGRect)rect {
glossRect.size.height = self.titleHeight;
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何困难,请告诉我.

还有原始博客的链接:http://dev.tuyennguyen.ca/?p = 298


THT*_*THT 5

我解决了这个问题.点击CalloutView中的任何内容,地图将无法触摸.我的calloutview是自定义的tabbleview

1 - 在文件MapviewController.h中,您将添加委托:UIGestureRecognizerDelegate

2 - 并在文件中MapViewController.m实现方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch

- 在我的mapView中,当你在地图上点击一次时,它会在这个方法中进行3次.所以我限制触摸将动作.第一次触摸将动作. - 在myCalloutView中有tabbleView,如果tabbleView接收触摸它将返回Map的虚假触摸,它会使你的tabbleview可以触摸.它对你的按钮也一样.

注意:在NSlog命中测试视图中:将具有您想要触摸的视图项的名称.示例我的视图:isEqualToString:@"UITableViewCellContentView"]

static int count=0;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    NSLog(@"hit test view %@",[touch view]);    
    if(count >0 && count<=2)
    {
        count++;
        count=count%2;
        return FALSE;
    }
    count++;      
    if ([[[[touch view] class] description] isEqualToString:@"UITableViewCellContentView"]) {
        return FALSE;
    }
    return TRUE;
}
Run Code Online (Sandbox Code Playgroud)