2015-10-20 76 views

回答

0

嘗試UITapGestureRecognizer

UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(mapDidTap:)]; 
[mapView addGestureRecognizer: tapGesture]; 

而且

-(void)mapDidTap:(UITapGestureRecognizer *)gestureRecognizer { 
    //do something when map is tapped 
} 
+0

我正在使用這個代碼,並且mapDidTap:永遠不會被調用。 –

0

下面的代碼檢測地圖的龍頭,在該位置增加了一個地圖標記:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITapGestureRecognizer *fingerTap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(handleMapFingerTap:)]; 
    fingerTap.numberOfTapsRequired = 1; 
    fingerTap.numberOfTouchesRequired = 1; 
    [self.mapView addGestureRecognizer:fingerTap]; 

} 

- (void)handleMapFingerTap:(UIGestureRecognizer *)gestureRecognizer { 

    NSLog(@"handleMapFingerTap gesture: %@", gestureRecognizer); 

    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) { 
     return; 
    } 

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView]; 
    CLLocationCoordinate2D touchMapCoordinate = 
    [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView]; 

    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init]; 
    pa.coordinate = touchMapCoordinate; 
    pa.title = @"Hello"; 
    [self.mapView addAnnotation:pa]; 

} 
0

我在我們的案例中,通過廣告修復了這個問題, 將手勢同步到註釋視圖和地圖視圖。

加點觸手勢映射

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.hideFilterView)) 
     self.mapView.addGestureRecognizer(tapGesture) 

    //override "viewFor annotation:" something like this 


    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

      var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin") 
      if annotationView == nil { 
       annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Pin" 

       annotationView?.canShowCallout = false 

      }else{ 

       annotationView?.annotation = annotation 

      } 
      let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.didTapPin(onMap:))) 
      annotationView?.addGestureRecognizer(tapGesture) 

    return annotationView 
    } 




//then don't override 

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
} 

所以所有引腳選擇將被處理使用自來水手勢。 你可以分別檢測地圖和別針。