2015-10-20 78 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) { 
} 

所以所有引脚选择将被处理使用自来水手势。 你可以分别检测地图和别针。