2010-08-03 4 views
22

임 MKMapView 및 MKAnnotationView에서 작업 중입니다.MKAnnotationView에서 CalloutBubble을 감지합니다.

지도에 특수 효과가 있습니다. 사용자가 그것을 탭하면 callOut Bubble이 표시됩니다. 주석을 다시 탭하면 (그리고 callOut Bubble이 표시됨) 다른보기로 변경해야합니다.

버블에서 두 번째 탭 또는 탭을 어떻게 찾을 수 있습니까?

+0

솔루션을 찾았습니까? –

+3

가장 간단한 방법은 버튼을 rightCalloutAccessoryView로 설정하고 calloutAccessoryControlTapped를 구현하는 것입니다. 그게 충분하지 않습니까? 아니면 제목과 부제목에서도 탭을 잡아야합니까? – Anna

답변

10

MKAnnotationView을 초기화 할 때 제스처 인식기를 추가 할 수 있습니까? 에 UITapGestureRecognizer 추가 사용자가 주석보기를 클릭 한 후

-(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; 
} 
+4

설명 선이 주석보기와 다릅니다. 설명 선은 설명 선보기를 누를 때 나타나는 풍선입니다. – shim

+6

mapView : didSelectAnnotationView : 메소드에 gestureRecognizer를 추가했으며이 솔루션은 완벽하게 작동했습니다. –

+0

@DanielT. MVP : P –

6

콜 아웃 버튼을 탭 : 여기

내부 dequeueReusableAnnotationViewWithIdentifier:

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

제스처 인식 방법에 대한 코드의 didAelectAnnotationView를 선택하십시오. 이렇게하면 액세서리보기를 사용하지 않고 설명 선을 탭할 수 있습니다.

그런 다음 추가 작업을 위해 보낸 사람으로부터 주석 개체를 다시 가져올 수 있습니다.

- (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]; 
    } 
} 
+2

이 솔루션에는주의하십시오. 여기서 문제는 주석을 선택할 때마다 탭 제스처를 작성하여 다중 탭 작성과 관련된 문제 (예 : 선택)를 선택 취소하고 다시 선택을 취소 할 수 있도록하는 것입니다. AnnotationView 초기화시 탭 동작을 추가하거나 선택 취소시 제스처 제거를 처리하는 것이 더 좋습니다. – Beninho85

4

UIButtonTypeDetailDisclosure 유형을 변경하지 않고 버튼의 맞춤 이미지를 설정해보세요. On를 사용하여, 3

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

좋은 것을 하나! 이런 슬픈 일은 API의 어느 곳에서도 문서화되지 않았습니다. – Cabus

6

여기에 다음 뷰 컨트롤러에 전달할 선택한 항목에서 얻는 데이터를 포함 Dhanu의 대답의 빠른 버전입니다. 처리해야합니다 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 
    } 
} 
0

스위프트 :

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