2011-06-23 133 views
6

我有一個mapkit應用程序,它可以在地圖上放置註釋,當你按下它時,它會顯示標題屬性的標註。ios mapkit通過點擊地圖關閉註釋標註

這工作正常,但用戶無法關閉它們。他們保持開放狀態,直到他們再次註釋爲止。我不能讓用戶點擊地圖上的其他地方(或再次點擊註釋)關閉它嗎?

我有一種感覺,這是默認設置,所以也許我正在做的東西是填塞它?我有我用它來檢測一些地圖水龍頭

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleTap:)]; 
tap.numberOfTapsRequired = 1; 

[self.mapView addGestureRecognizer: tap]; 

它將觸發該手勢識別:

- (void)handleTap:(UITapGestureRecognizer *)sender {  
if (sender.state == UIGestureRecognizerStateEnded) { 


    CGPoint tapPoint = [sender locationInView:sender.view.superview]; 
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint: tapPoint toCoordinateFromView: self.mapView]; 

    if (pitStopMode && !pitStopMade){ 
      pitStopMade = YES; 
     InfoAnnotation *annotation = [[InfoAnnotation alloc] 
         initNewPitstopWithCoordinate:coordinate]; 
     NSLog(@" Created Pit Stop"); 

     annotation.draggable = NO; 
     //place it on the map 
     [self.mapView addAnnotation: annotation]; 

     self.instructionLabel.text = @"Tap button again to remove"; 
     annotation.creatorId = self.localUser.deviceId; 
     //send it to the server 
     [annotation updateLocationWithServerForConvoy: self.convoyCode];   

     [annotation release]; 

    } 

    if (hazardMode && !hazardMade){ 
      hazardMade = YES; 
     InfoAnnotation *annotation = [[InfoAnnotation alloc] 
         initNewHazardWithCoordinate:coordinate];   
     NSLog(@" Created Hazard"); 

     annotation.draggable = NO; 
     //place it on the map 
     [self.mapView addAnnotation: annotation]; 

     self.instructionLabel.text = @"Tap button again to remove"; 
     annotation.creatorId = self.localUser.deviceId; 
     //send it to the server 
     [annotation updateLocationWithServerForConvoy: self.convoyCode];   

     [annotation release]; 


    } 
} 

}

有什麼我必須做的,也讓這些水龍頭經歷到mapview?在註釋上拖動和點擊效果很好,但我不確定這是什麼原因造成的?

有沒有我缺少的選項,還是我必須嘗試手動實現這個?

回答

10

您可以實施UIGestureRecognizerDelegate方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:並返回YES(因此地圖視圖的自己的輕擊手勢識別器也將執行其方法)。

首先,協議聲明添加到您的視圖控制器的接口(以避免編譯器警告):

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate> 

接下來,設置delegate財產上的手勢識別:

tap.delegate = self; 

最後,實施方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer: 
     (UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return YES; 
} 



如果不因某種原因失效了,你可以手動或者在handleTap:方法的頂部取消選擇任何當前選擇的註釋:

for (id<MKAnnotation> ann in mapView.selectedAnnotations) { 
    [mapView deselectAnnotation:ann animated:NO]; 
} 

即使地圖視圖只允許最多一次選擇一個註釋,selectedAnnotations屬性爲NSArray,所以我們通過它循環。

0

安娜解釋好,我也想用完全的代碼解釋。你可以這樣做

 UITapGestureRecognizer *tapMap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeCallout:)]; 
     [self.mapView addGestureRecognizer:tapMap]; 

-(void) closeCallout:(UIGestureRecognizer*) recognizer 
     { 
      for (id<MKAnnotation> ann in mapView.selectedAnnotations) 
      { 
       [mapView deselectAnnotation:ann animated:NO]; 
      }  


     }