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

时间:2012-11-21 17:18:53

标签: ios mapkit uigesturerecognizer geometry

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

我想要做的是,允许用户在触摸地图时关闭MKCircle。但是,如果用户触摸任何其他引脚或MKCircle本身,我希望MKCircle不要忽略。

有什么想法吗?

这是我当前的代码,当触摸地图的任何部分时,它会解除MKCircle

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

2 个答案:

答案 0 :(得分:5)

deactivateAllRadars方法中,您可以使用hitTest:withEvent:来判断是否已点击MKAnnotationView

How can I catch tap on MapView and then pass it to default gesture recognizers?中显示了一个例子(这是第二个代码示例)。

如果点击注释,您可以避免删除圈子。

如果尚未点按注释,则可以通过获取触摸坐标来检查是否点击了MKCircle(请参阅How to capture Tap gesture on MKMapView以获取示例)并查看距离触摸的距离圆的中心大于其半径。

请注意,deactivateAllRadars应更改为deactivateAllRadars:(UITapGestureRecognizer *)tgr,因为它需要来自相关手势识别器的信息。还要确保在方法选择器的末尾添加冒号,在其中分配+ 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...
            }
        }
    }
}

答案 1 :(得分:3)

这是我的 Swift 2.1 兼容版本:

driver.get("http://en.boerse-frankfurt.de/")
//make some wait here to fully load the browser.

driver.find_element_by_xpath(".//*[@id='searchvalue']").click()
search_box.send_keys(search_string)

MKNewAnnotationContainerView 是一个私人内部类,所以无法直接比较,如:

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")
        }
    }
}