我正在使用 MKMapView 和 MKAnnotationView。
我在地图上有注释。当用户点击它时,会显示 callOut Bubble。当再次点击注释时(并且 callOut Bubble 可见),我需要更改为另一个视图。
我如何检测第二次点击,或气泡中的点击?
我正在使用 MKMapView 和 MKAnnotationView。
我在地图上有注释。当用户点击它时,会显示 callOut Bubble。当再次点击注释时(并且 callOut Bubble 可见),我需要更改为另一个视图。
我如何检测第二次点击,或气泡中的点击?
初始化时可以添加手势识别器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;
}
这是 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)
}
要在用户单击 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];
}
}
尝试在不更改 UIButtonTypeDetailDisclosure 类型的情况下为按钮设置自定义图像。
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
这是我对这个问题的解决方案:
斯威夫特 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
}
这适用于 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")
}
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
}
}