我正在使用MKMapView和MKAnnotationView。
我在地图上有一个注释。当用户点击它时,会显示callOut Bubble。再次点击注释(并且callOut Bubble可见)我需要更改为另一个视图。
如何检测第二个水龙头或气泡中的水龙头?
答案 0 :(得分:11)
初始化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;
}
答案 1 :(得分: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)
}
答案 2 :(得分: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];
}
}
答案 3 :(得分:4)
尝试在不更改UIButtonTypeDetailDisclosure类型的情况下为按钮设置自定义图像。
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
答案 4 :(得分: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
}
}
答案 5 :(得分:0)
这是我对这个问题的解决方案:
快速5
首先,添加MKMapViewDelegate
方法
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
在选择注解并显示标注气泡时调用此选项。您可以像这样提取标注气泡视图,然后向其中添加点击手势识别器:
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
}
答案 6 :(得分:0)
这在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")
}