2011-12-19 52 views
12

Tôi có một bản đồ và một bộ chú thích, mỗi chú thích có thuộc tính 'cha mẹ'. Hiện tại khi tôi thêm chú thích, tôi triển khai phương thức didAddAnnotationViews để tạo hoạt ảnh cho các chú thích đó để chúng xuất hiện từ tọa độ của cha mẹ chúng. Có cách nào để thực hiện việc này trong khi xóa chú thích không? Khi tôi xóa chú thích khỏi bản đồ, tôi muốn chú thích đó hoạt ảnh với tọa độ gốc của nó và theo như tôi biết, không có tương đương với didAddAnnotationViews khi chú thích bị xóa.Xóa động các chú thích

Trả lời

15

Chú thích animate trước bạn xóa nó khỏi bản đồ và thực hiện xóa sau khi hoạt ảnh được hoàn tất. Mã này có thể trông giống như:

- (void) removeMyAnnotation:(MyAnnotation*)annotation{ 
    [UIView animateWithDuration:1.0f 
        animations:^(void){ 
         annotation.coordinate = annotation.parentAnnotation.coordinate; 
        } 
        completion:^(BOOL finished)completion{ 
         [mapView removeAnnotation:annotation]; 
        } 
} 
+0

Cảm ơn - một điều khác, là có một cách truy cập MKAnnotationView tương ứng của chú thích để tôi có thể di chuyển nó theo cách đó? Bởi vì lớp chú thích của tôi tính toán tọa độ động của nó nên nó không thể được thiết lập (trừ khi tôi thêm vào một tải các chức năng để thiết lập các tọa độ tùy chỉnh, mà tôi muốn tránh). – benwad

+2

@benwad, bạn có thể sử dụng phương thức viewForAnnotation trong MKMapView, mặc dù nó không được đảm bảo luôn trả về dạng xem thực (nó có thể trả về nil nếu chú thích không hiển thị ví dụ) – Vladimir

+0

ParentAnnotation là gì? – malhal

5

Bạn nên KHÔNG gọi Hoãn của removeAnnotation như trong câu trả lời @ Vladimir vì tình trạng MKMapView có thể thay đổi trong hình ảnh động.

Khi thời gian removeAnnotation được gọi từ khối hoàn thành hoạt ảnh, một chú thích khác có thể được thêm/xóa khỏi MapView - vì vậy, trong một số trường hợp, bạn có thể kết thúc xóa nhóm chú thích sai.

tôi đã viết thể loại này cho MKMapView bạn có thể sử dụng để loại bỏ chú thích hoạt hình theo cách an toàn:

@interface MKMapView (RemoveAnnotationWithAnimation) 

- (void)removeAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated; 
- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)animated; 

@end 

Và file .m:

@implementation MKMapView (RemoveAnnotationWithAnimation) 

- (void)removeAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated 
{ 
    [self removeAnnotations:@[annotation] animated:animated]; 
} 

- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)animated 
{ 
    if (animated) { 
     NSSet * visibleAnnotations = [self annotationsInMapRect:self.visibleMapRect]; 
     NSMutableArray * annotationsToRemoveWithAnimation = [NSMutableArray array]; 
     for (id<MKAnnotation> annotation in annotations) { 
      if ([visibleAnnotations containsObject:annotation]) { 
       [annotationsToRemoveWithAnimation addObject:annotation]; 
      } 
     } 
     NSMutableArray * snapshotViews = [NSMutableArray array]; 
     for (id<MKAnnotation> annotation in annotationsToRemoveWithAnimation) { 
      UIView * annotationView = [self viewForAnnotation:annotation]; 
      if (annotationView) { 
       UIView * snapshotView = [annotationView snapshotViewAfterScreenUpdates:NO]; 
       snapshotView.frame = annotationView.frame; 
       [snapshotViews addObject:snapshotView]; 
       [[annotationView superview] insertSubview:snapshotView aboveSubview:annotationView]; 
      } 
     } 
     [UIView animateWithDuration:0.5 
         animations:^{ 
          for (UIView * snapshotView in snapshotViews) { 
           // Change the way views are animated if you want 
           CGRect frame = snapshotView.frame; 
           frame.origin.y = -frame.size.height; 
           snapshotView.frame = frame; 
          } 
         } 
         completion:^(BOOL finished) { 
          for (UIView * snapshotView in snapshotViews) { 
           [snapshotView removeFromSuperview]; 
          } 
         }]; 
    } 
    [self removeAnnotations:annotations]; 
} 

@end 
+0

Tôi đã triển khai giải pháp này để làm mờ chú thích của mình. Điều này không hoạt động khi panning bản đồ trong khi các khung nhìn snapshot đang mờ dần, vì chúng sẽ được đặt trong khi bản đồ di chuyển. Trông khá kỳ lạ. – duncanc4

+0

Sau khi đọc qua nhận xét của bạn và suy nghĩ về giải pháp của @ Vladimir, tôi không thấy cách thêm hoặc loại bỏ các chú thích khác nhau trong các hiệu ứng hoạt hình khả năng loại bỏ chú thích cụ thể khi khối hoàn thành thực hiện. Khối này nắm bắt con trỏ, vì vậy nó có chú thích chính xác cần xóa. Vấn đề duy nhất tôi có thể thấy là nếu một số mã khác loại bỏ chú thích chính xác đang hoạt ảnh. Tôi không chắc chắn những gì 'MKMapView' hiện nếu nó không có chú thích bạn đang cố gắng để loại bỏ. – duncanc4

+0

@ duncanc4 Vấn đề là MKMapView sử dụng lại các trường hợp xem chú thích, như UITableView làm với các ô của nó. Vì vậy, nếu bạn lưu con trỏ vào chú thích bị xóa và di chuyển bản đồ đến nhóm chú thích khác, bạn có thể kết thúc xóa chú thích sai khi khối thực thi. – NAlexN

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