How to add touch gesture to map but ignore touches on pins and annotations?

后端 未结 2 1767
孤街浪徒
孤街浪徒 2021-01-05 05:57

I have a mapview that uses MKCircles to display radius information for certain user actions.

What I want to do, is allow the user to dismiss the M

2条回答
  •  不思量自难忘°
    2021-01-05 06:18

    In the deactivateAllRadars method, you can use hitTest:withEvent: to tell whether an MKAnnotationView has been tapped or not.

    An example of this is shown in How can I catch tap on MapView and then pass it to default gesture recognizers? (it's the second code sample).

    This will let you avoid removing the circle if an annotation has been tapped.

    If an annotation has not been tapped, you can then check if an MKCircle was tapped by getting the coordinates of the touch (see How to capture Tap gesture on MKMapView for an example) and seeing if the distance from the touch to the circle's center is greater than its radius.

    Note that the deactivateAllRadars should be changed to deactivateAllRadars:(UITapGestureRecognizer *)tgr because it will need information from the associated gesture recognizer. Also be sure to add a colon at the end of the method's selector where you alloc+init tap.

    For example:

    -(void)deactivateAllRadars:(UITapGestureRecognizer *)tgr
    {
        CGPoint p = [tgr locationInView:mapView];
    
        UIView *v = [mapView hitTest:p withEvent:nil];
    
        id 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...
                }
            }
        }
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题