检测点击MKAnnotationView中的CalloutBubble

Ale*_*lez 25 mkmapview mkannotation mkannotationview ios

我正在使用MKMapView和MKAnnotationView.

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

如何检测第二个水龙头或气泡中的水龙头?

Dol*_*lbz 11

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

这是里面的代码 dequeueReusableAnnotationViewWithIdentifier:

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

手势识别器的方法:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我在mapView:didSelectAnnotationView:方法中添加了gestureRecognizer,这个解决方案非常适合我. (6认同)
  • 标注与注释视图不同.标注是点击标注视图时显示的气泡. (4认同)
  • 你需要使用这个:func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) (3认同)

Kei*_*ith 7

这是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)
}
Run Code Online (Sandbox Code Playgroud)


小智 6

要在用户单击Annotation视图后点击callout按钮,请在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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 小心这个解决方案.这里的问题是,每次选择注释时都会创建一个点击手势,因此您可能会遇到多个点击创建的问题,例如,如果您选择它,请取消选择并重新选择它.我认为最好在AnnotationView初始化时添加点击手势或在取消选择时处理手势删除 (2认同)

Mat*_*ler 5

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

UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];        
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)