2010-08-03 34 views
22

Im làm việc với MKMapView và MKAnnotationView.Phát hiện Tap trên CalloutBubble trong MKAnnotationView

Tôi có chú thích trên bản đồ. Khi người dùng chạm vào nó, bong bóng callOut sẽ được hiển thị. Khi chú thích được nhấn lại lần nữa (và bong bóng callOut hiển thị), tôi cần thay đổi sang chế độ xem khác.

Làm cách nào tôi có thể phát hiện lần nhấn thứ hai hoặc nhấn vào bong bóng?

+0

Bạn đã tìm thấy giải pháp chưa? –

+3

Cách đơn giản nhất là đặt nút làm rightCalloutAccessoryView và triển khai calloutAccessoryControlTapped. Đó có phải là không đủ hoặc bạn phải bắt vòi trên tiêu đề và phụ đề là tốt? – Anna

Trả lời

10

Bạn có thể thêm trình nhận dạng cử chỉ khi đang khởi tạo MKAnnotationView không?

Dưới đây là các mã cho bên dequeueReusableAnnotationViewWithIdentifier:

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

Phương pháp cho nhận dạng cử chỉ:

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

Chú dẫn không giống với chế độ xem chú thích. Chú thích là bong bóng xuất hiện khi bạn nhấn vào chế độ xem chú thích. – shim

+6

Tôi đã thêm gestureRecognizer vào phương thức mapView: didSelectAnnotationView: và giải pháp này hoạt động hoàn hảo cho tôi. –

+0

@DanielT. Bạn các MVP: P –

6

Để khai thác các nút callout sau khi người dùng đã nhấp vào xem chú thích, thêm một UITapGestureRecognizer trong didSelectAnnotationView. Bằng cách này, bạn có thể triển khai nhấn vào chú thích mà không cần các chế độ xem phụ kiện.

Sau đó, bạn có thể nhận lại đối tượng chú thích từ người gửi để thực hiện thêm hành động.

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

Hãy cẩn thận với giải pháp này. Vấn đề ở đây là bạn tạo một cử chỉ chạm mỗi khi bạn chọn một chú thích để bạn có thể gặp vấn đề với việc tạo nhiều thao tác nhấn, ví dụ nếu bạn chọn nó, hãy bỏ chọn nó và chọn lại nó. Tôi nghĩ sẽ tốt hơn khi thêm một cử chỉ chạm vào lúc khởi tạo AnnotationView hoặc xử lý xóa cử chỉ khi bỏ chọn – Beninho85

4

Cố gắng đặt hình ảnh tùy chỉnh cho nút mà không thay đổi loại UIButtonTypeDetailDisclosure.

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

Tốt nhất! Thật buồn khi những thứ như thế này không được ghi lại ở bất kỳ đâu trong API -.- – Cabus

6

Dưới đây là phiên bản nhanh chóng của câu trả lời Dhanu, trong đó có nhận được dữ liệu từ các sản phẩm được chọn để vượt qua với bộ điều khiển xem tiếp theo:

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

Swift 3, sử dụng On. Bạn cần xử lý 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 
    } 
} 
Các vấn đề liên quan