检测点击MKAnnotationView中的CalloutBubble

我正在使用MKMapView和MKAnnotationView。

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

我如何检测第二个水龙头,或在泡沫龙头?

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

这里是dequeueReusableAnnotationViewWithIdentifier:的代码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; } 

要在用户点击注解视图后点击标注button,请在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]; } } 

这里是Dhanu的答案的快速版本,包括从select的项目中获取数据传递给下一个视图控制器:

 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) } 

尝试在不更改UIButtonTypeDetailDisclosuretypes的情况下为button设置自定义图像。

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

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 } }