我正在使用MKMapView和MKAnnotationView。在MKAnnotationView的CalloutBubble上檢測點擊
我在地圖上有一個註釋。當用戶點擊它時,會顯示callOut Bubble。當再次敲擊註釋(並且callOut Bubble可見)時,我需要切換到另一個視圖。
我該如何檢測第二次水龍頭或水龍頭中的水龍頭?
我正在使用MKMapView和MKAnnotationView。在MKAnnotationView的CalloutBubble上檢測點擊
我在地圖上有一個註釋。當用戶點擊它時,會顯示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;
}
該標註與註釋視圖不同。標註是您點按標註視圖時顯示的氣泡。 – shim 2014-02-07 04:20:58
我在mapView:didSelectAnnotationView:方法中添加了gestureRecognizer,此解決方案對我來說非常合適。 – 2014-05-20 19:30:47
@DanielT。你需要使用MVP:P – 2014-06-24 09:40:27
挖掘標註按鈕,用戶點擊了註釋視圖後,添加一個UITapGestureRecognizer在didSelectAnnotationView。通過這種方式,您可以在不需要附件視圖的情況下輕敲標註。
然後,您可以從發件人處取回註釋對象以進行進一步操作。
- (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];
}
}
請小心使用此解決方案。這裏的問題在於,每次選擇註釋時都會創建一個輕擊手勢,以便您可以在創建多個輕敲時遇到問題,例如,如果您選擇它,取消選擇並重新選擇它。我認爲最好在AnnotationView初始化時添加一個輕擊手勢,或者在取消選擇處理手勢刪除 – Beninho85 2016-09-22 18:01:55
嘗試在不更改UIButtonTypeDetailDisclosure類型的情況下爲按鈕設置自定義圖像。
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
好的!所以這樣的東西在API的任何地方都沒有記錄,所以很傷心 - – Cabus 2015-10-16 18:20:33
這裏的SWIFT版本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)
}
斯威夫特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
}
}
您是否找到解決方案? – 2011-01-10 07:56:02
最簡單的方法是將按鈕設置爲rightCalloutAccessoryView並實現calloutAccessoryControlTapped。這是不夠的,或者你必須抓住標題和副標題? – Anna 2011-10-12 02:06:49