在MKAnnotationView的CalloutBubble上检测点击

问题描述:

我正在使用MKMapView和MKAnnotationView。在MKAnnotationView的CalloutBubble上检测点击

我在地图上有一个注释。当用户点击它时,会显示callOut Bubble。当再次敲击注释(并且callOut Bubble可见)时,我需要切换到另一个视图。

我该如何检测第二次水龙头或水龙头中的水龙头?

+0

您是否找到解决方案? – 2011-01-10 07:56:02

+3

最简单的方法是将按钮设置为rightCalloutAccessoryView并实现calloutAccessoryControlTapped。这是不够的,或者你必须抓住标题和副标题? – Anna 2011-10-12 02:06:49

当您初始化MKAnnotationView时,您可以添加手势识别器吗?

下面是内部dequeueReusableAnnotationViewWithIdentifier:

UITapGestureRecognizer *tapGesture = 
     [[UITapGestureRecognizer alloc] initWithTarget:self 
             action:@selector(calloutTapped:)]; 
[theAnnotationView addGestureRecognizer:tapGesture]; 
[tapGesture release]; 

用于手势识别的方法的代码:

-(void) calloutTapped:(id) sender { 
    // code to display whatever is required next. 

    // To get the annotation associated with the callout that caused this event: 
    // id<MKAnnotation> annotation = ((MKAnnotationView*)sender.view).annotation; 
} 
+4

该标注与注释视图不同。标注是您点按标注视图时显示的气泡。 – shim 2014-02-07 04:20:58

+6

我在mapView:didSelectAnnotationView:方法中添加了gestureRecognizer,此解决方案对我来说非常合适。 – 2014-05-20 19:30:47

+0

@DanielT。你需要使用MVP:P – 2014-06-24 09:40:27

挖掘标注按钮,用户点击了注释视图后,添加一个UITapGestureRecognizer在didSelectAnnotationView。通过这种方式,您可以在不需要附件视图的情况下轻敲标注。

然后,您可以从发件人处取回注释对象以进行进一步操作。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)]; 
    [view addGestureRecognizer:tapGesture]; 
} 

-(void)calloutTapped:(UITapGestureRecognizer *) sender 
{ 
    NSLog(@"Callout was tapped"); 

    MKAnnotationView *view = (MKAnnotationView*)sender.view; 
    id <MKAnnotation> annotation = [view annotation]; 
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) 
    { 
     [self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation]; 
    } 
} 
+2

请小心使用此解决方案。这里的问题在于,每次选择注释时都会创建一个轻击手势,以便您可以在创建多个轻敲时遇到问题,例如,如果您选择它,取消选择并重新选择它。我认为最好在AnnotationView初始化时添加一个轻击手势,或者在取消选择处理手势删除 – Beninho85 2016-09-22 18:01:55

尝试在不更改UIButtonTypeDetailDisclosure类型的情况下为按钮设置自定义图像。

UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];   
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal]; 
+1

好的!所以这样的东西在API的任何地方都没有记录,所以很伤心 - – Cabus 2015-10-16 18:20:33

这里的SWIFT版本Dhanu的回答,包括选择用来传递到下一个视图控制器的项目中获取数据:

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
    let gesture = UITapGestureRecognizer(target: self, action: #selector(MyMapViewController.calloutTapped(_:))) 
    view.addGestureRecognizer(gesture) 
} 

func calloutTapped(sender:UITapGestureRecognizer) { 
    guard let annotation = (sender.view as? MKAnnotationView)?.annotation as? MyAnnotation else { return } 

    selectedLocation = annotation.myData 
    performSegueWithIdentifier("mySegueIdentifier", sender: self) 
} 

斯威夫特3,使用On。您需要处理rightCalloutAccessoryView

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    switch annotation { 
    case let annotation as Annotation: 
    let view: AnnotationView = mapView.dequeue(annotation: annotation) 
    view.canShowCallout = true 
    let button = UIButton(type: .detailDisclosure) 
    button.on.tap { [weak self] in 
     self?.handleTap(annotation: annotation) 
    } 
    view.rightCalloutAccessoryView = button 
    return view 
    default: 
    return nil 
    } 
}