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

Jim*_*med 7 geometry mapkit uigesturerecognizer ios

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

我想做的是允许用户MKCircle在他们触摸地图时解雇.但是,如果MKCircle用户触摸任何其他引脚或其MKCircle自身,我希望不要忽略.

有任何想法吗?

这是我当前的代码,它解释了MKCircle触摸地图的任何部分的时间:

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

小智 5

在该deactivateAllRadars方法中,您可以hitTest:withEvent:用来判断是否MKAnnotationView已经点击了.

这方面的例子中显示我如何能赶上水龙头上的MapView,然后把它传递到默认手势识别?(这是第二个代码示例).

如果已点击注释,这将允许您避免删除圆圈.

如果尚未点击注释,则可以MKCircle通过获取触摸坐标来检查是否已触摸(例如,参见如何捕获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...
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)