28

我正在使用 MKMapView 和 MKAnnotationView。

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

我如何检测第二次点击,或气泡中的点击?

4

7 回答 7

12

初始化时可以添加手势识别器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;
}
于 2011-10-13T12:39:32.320 回答
10

这是 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)
}
于 2016-06-09T12:09:41.283 回答
6

要在用户单击 Annotation 视图后点击标注按钮,请在 didSelectAnnotationView 中添加一个 UITapGestureRecognizer。这样,您可以在不需要附件视图的情况下实现对标注的点击。

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

- (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];
    }
}
于 2014-11-06T01:23:55.410 回答
5

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

UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];        
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
于 2015-02-24T00:34:12.867 回答
1

这是我对这个问题的解决方案:

斯威夫特 5

一、添加MKMapViewDelegate方法

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)

This gets called when annotation is selected and the callout bubble is shown. 您可以像这样提取标注气泡视图,并将点击手势识别器添加到其中,如下所示:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // Set action to callout view
    guard let calloutView = view.subviews.first else { return }
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userDidTapAnnotationCalloutView(_:)))
    calloutView.addGestureRecognizer(tapGesture)
}

并像这样处理点击动作:

@objc
private func userDidTapAnnotationCalloutView(_ sender: UITapGestureRecognizer) {
    // Handle tap on callout here
}
于 2019-12-12T10:38:03.027 回答
1

这适用于 Swift 5.2

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

@objc func calloutTapped() {
    
    print ("Callout Tapped")
    
}
于 2020-10-29T13:13:59.290 回答
0

Swift 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
  }
}
于 2017-07-10T21:12:18.750 回答