如何添加触摸手势映射,但忽略引脚和注释触摸?

我有一个使用MKCircle s来显示某些用户操作的半径信息的mapview。

我想要做的就是允许用户在触摸地图时closuresMKCircle 。 不过,我希望MKCircle不要解雇,如果用户触摸任何其他针或MKCircle本身。

有任何想法吗?

这里是我当前的代码,当地图的任何部分被触摸时,它将closuresMKCircle

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deactivateAllRadars)]; [tap setCancelsTouchesInView:NO]; [_mapView addGestureRecognizer:tap]; 

deactivateAllRadars方法中,可以使用hitTest:withEvent:来判断是否已经轻敲MKAnnotationView

我怎样才能捕捉到MapView,然后将其传递给默认的手势识别器呢? (这是第二个代码示例)。

这可以让你避免如果一个注释被轻敲去除圆。

如果没有点击注释,则可以通过获取触摸的坐标来检查是否轻敲了MKCircle (例如,请参阅如何在MKMapView上捕获点击手势 ),并查看触摸到圆心的距离是大于其半径。

请注意,应将deactivateAllRadars更改为deactivateAllRadars:(UITapGestureRecognizer *)tgr因为它需要来自关联手势识别器的信息。 还要确保在方法的select器的末尾添加一个冒号,在那里你分配+ init的tap

例如:

 -(void)deactivateAllRadars:(UITapGestureRecognizer *)tgr { CGPoint p = [tgr locationInView:mapView]; UIView *v = [mapView hitTest:p withEvent:nil]; id<MKAnnotation> ann = nil; if ([v isKindOfClass:[MKAnnotationView class]]) { //annotation view was tapped, select it... ann = ((MKAnnotationView *)v).annotation; [mapView selectAnnotation:ann animated:YES]; } else { //annotation view was not tapped, deselect if some ann is selected... if (mapView.selectedAnnotations.count != 0) { ann = [mapView.selectedAnnotations objectAtIndex:0]; [mapView deselectAnnotation:ann animated:YES]; } //remove circle overlay if it was not tapped... if (mapView.overlays.count > 0) { CGPoint touchPoint = [tgr locationInView:mapView]; CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; CLLocation *touchLocation = [[CLLocation alloc] initWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude]; CLLocation *circleLocation = [[CLLocation alloc] initWithLatitude:circleCenterLatitude longitude:circleCenterLongitude]; CLLocationDistance distFromCircleCenter = [touchLocation distanceFromLocation:circleLocation]; if (distFromCircleCenter > circleRadius) { //tap was outside the circle, call removeOverlay... } } } } 

这是我的Swift 2.1兼容版本:

 func didTapOnMap(recognizer: UITapGestureRecognizer) { let tapLocation = recognizer.locationInView(self) if let subview = self.hitTest(tapLocation, withEvent: nil) { if subview.isKindOfClass(NSClassFromString("MKNewAnnotationContainerView")!) { print("Tapped out") } } } 

MKNewAnnotationContainerView是一个私有的内部类,所以你不能直接比较像:

 if subview is MKNewAnnotationContainerView { }