2012-06-29 28 views
6

Nhiệm vụ của tôi là bỏ chọn chú thích bản đồ trên lần nhấn thứ hai.Cách bỏ chọn chú thích bản đồ trên lần nhấn thứ hai

Tôi không tìm thấy cách thực hiện với chức năng mapView. Vì vậy, tôi đã sử dụng một bài viết từ stackoverflow và làm như thế này:

- (void)viewDidLoad 
{ 
    annotationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationTapRecognized:)]; 
    annotationTap.numberOfTapsRequired = 1; 
    annotationTap.delegate = self; 
} 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    [view addGestureRecognizer:annotationTap]; 
} 

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{ 
    [view removeGestureRecognizer:annotationTap]; 
} 

- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture 
{ 
    NSArray *selectedAnnotations = self.viewMap.selectedAnnotations; 
    for (MapAnnotation *annotationView in selectedAnnotations) { 
     [self.viewMap deselectAnnotation:annotationView animated:NO]; 
    } 
} 

Có vẻ như nó hoạt động chính xác, nhưng không phải vậy. Khi tôi nhấn vào chú thích thứ hai chú thích thời gian biến mất và xuất hiện một lần nữa.

Bất kỳ ý tưởng nào?

Xin cảm ơn trước.

Trả lời

18

Tôi đã tìm thấy giải pháp. Có lẽ nó không tốt.

Tôi đã thêm boolean "is show", như luxsypher đã đề cập. Vì vậy, chức năng của tôi trông giống như sau:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    [view addGestureRecognizer:annotationTap]; 

    if (isShow) { 
     NSArray *selectedAnnotations = self.viewMap.selectedAnnotations; 
     for (MapAnnotation *annotationView in selectedAnnotations) { 
      [self.viewMap deselectAnnotation:annotationView animated:YES]; 
     } 
     isShow = FALSE; 
    } 
} 

- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture 
{ 
    NSArray *selectedAnnotations = self.viewMap.selectedAnnotations; 
    for (MapAnnotation *annotationView in selectedAnnotations) { 
     [self.viewMap deselectAnnotation:annotationView animated:YES]; 
    } 
    isShow = TRUE; 
} 

Có thể nó sẽ hữu ích cho ai đó :).

Cảm ơn.

1

Có thể bạn nên thêm một boolean "có thể nhìn thấy" và hành động do đó. Nguyên nhân có vẻ như cử chỉ của bạn được gọi và sau đó "đã chọn" được gọi lại.

+0

Nhưng tôi đã thêm thông tin này ở đâu? Tôi đã nghĩ về điều này. Nhưng làm cách nào tôi có thể cấm hiển thị chú dẫn khi nhấp chuột? Cảm ơn. – Igor

Các vấn đề liên quan