2014-06-12 19 views
5

Tôi có một lớp con tùy chỉnh là MKPinAnnotationView hiển thị một cuộc gọi tùy chỉnh. Tôi muốn xử lý các sự kiện cảm ứng bên trong chú thích đó.Ngăn chặn lựa chọn thay đổi MKMapView (sạch)

Tôi có giải pháp làm việc (bên dưới) nhưng nó không cảm thấy đúng. Tôi có một quy tắc chung là bất cứ khi nào tôi sử dụng performSelector: .. withDelay: Tôi đang chiến đấu với hệ thống thay vì làm việc với nó.

Có ai có giải pháp tốt, sạch sẽ để xử lý sự kiện tích cực của việc xử lý lựa chọn MKMapView và chú thích không?

giải pháp hiện tại của tôi:

(Tất cả các mã từ lớp lựa chọn chú thích của tôi)

tôi làm thử nghiệm hit của riêng tôi (không có này recognisers cử chỉ của tôi không bắn như xem bản đồ tiêu thụ các sự kiện :

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event; { 
    // To enable our gesture recogniser to fire. we have to hit test and return the correct view for events inside the callout. 

    UIView* hitView = nil; 

    if (self.selected) { 
     // check if we tpped inside the custom view 
     if (CGRectContainsPoint(self.customView.frame, point)) 
      hitView = self.customView; 
    } 

    if(hitView) { 
     // If we are performing a gesture recogniser (and hence want to consume the action) 
     // we need to turn off selections for the annotation temporarily 
     // the are re-enabled in the gesture recogniser. 
     self.selectionEnabled = NO; 

     // *1* The re-enable selection a moment later 
     [self performSelector:@selector(enableAnnotationSelection) withObject:nil afterDelay:kAnnotationSelectionDelay]; 

    } else { 
     // We didn't hit test so pass up the chain. 
     hitView = [super hitTest:point withEvent:event]; 
    } 

    return hitView; 
} 

Lưu ý rằng tôi cũng tắt các lựa chọn để trong tôi ghi đè setSelected tôi có thể bỏ qua các deselection

0.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated; { 
    // If we have hit tested positive for one of our views with a gesture recogniser, temporarily 
    // disable selections through _selectionEnabled 
    if(!_selectionEnabled){ 
     // Note that from here one, we are out of sync with the enclosing map view 
     // we're just displaying out callout even though it thinks we've been deselected 
     return; 
    } 

    if(selected) { 
     // deleted code to set up view here 
     [self addSubview:_customView]; 

     _mainTapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)]; 
     [_customView addGestureRecognizer: _mainTapGestureRecogniser]; 

    } else { 
     self.selected = NO; 
     [_customView removeFromSuperview]; 
    } 


} 

Đó là dòng đã nhận xét mà tôi không thích nhưng nó cũng khá lông vào cuối theta cháy chậm. Tôi phải đi ngược lại chuỗi giám sát để đến mapView để tôi có thể thuyết phục nó rằng lựa chọn vẫn còn đúng chỗ.

// Locate the mapview so that we can ensure it has the correct annotation selection state after we have ignored selections. 
- (void)enableAnnotationSelection { 
    // This reenables the seelction state and resets the parent map view's idea of the 
    // correct selection i.e. us. 
    MKMapView* map = [self findMapView]; 
    [map selectAnnotation:self.annotation animated:NO]; 
    _selectionEnabled = YES; 

} 

với

-(MKMapView*)findMapView; { 
    UIView* view = [self superview]; 
    while(!_mapView) { 
     if([view isKindOfClass:[MKMapView class]]) { 
      _mapView = (MKMapView*)view; 
     } else if ([view isKindOfClass:[UIWindow class]]){ 
      return nil; 
     } else{ 
      view = [view superview]; 
      if(!view) 
       return nil; 
     } 
    } 

    return _mapView; 
} 

này tất cả dường như làm việc mà không và nhược điểm (như nhấp nháy tôi đã nhìn thấy từ các giải pháp khác. Đó là tương đối đơn giản nhưng nó không cảm thấy đúng.

Bất cứ ai có một giải pháp tốt hơn không?

Trả lời

1

Tôi không nghĩ bạn cần phải theo dõi lựa chọn của chế độ xem bản đồ. Nếu tôi giải thích chính xác những gì bạn muốn, bạn sẽ có thể hoàn thành nó chỉ với ghi đè hitTest:withEvent:canShowCallout được đặt thành NO. Trong setSelected: thực hiện hoạt ảnh chú thích/hoạt ảnh biến mất của bạn cho phù hợp. Bạn cũng nên ghi đè setHighlighted: và điều chỉnh hiển thị chú dẫn tùy chỉnh của bạn nếu hiển thị.

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