是CGPoint在MKPolygonView?(Is CGPoint in MKPolygonVie

2019-10-28 21:14发布

我试图找出一种方法来检测其MKOverlayView (实际上MKPolygonView )被窃听,然后改变它的颜色。

我得到了它与此代码运行:

- (void)mapTapped:(UITapGestureRecognizer *)recognizer {

    MKMapView *mapView = (MKMapView *)recognizer.view;
    MKPolygonView *tappedOverlay = nil;
    for (id<MKOverlay> overlay in mapView.overlays)
    {
        MKPolygonView *view = (MKPolygonView *)[mapView viewForOverlay:overlay];

        if (view){
            // Get view frame rect in the mapView's coordinate system
            CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
            // Get touch point in the mapView's coordinate system
            CGPoint point = [recognizer locationInView:mapView];
            // Check if the touch is within the view bounds
            if (CGRectContainsPoint(viewFrameInMapView, point))
            {

                tappedOverlay = view;
                break;
            }
        }
    }

    if([[tappedOverlay fillColor] isEqual:[[UIColor cyanColor] colorWithAlphaComponent:0.2]]){
        [listOverlays addObject:tappedOverlay];
        tappedOverlay.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
    }
    else{
        [listOverlays removeObject:tappedOverlay];
        tappedOverlay.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
    }
    //tappedOverlay.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];

}

这但有时工作,这取决于在那里我点击它得到错误的这MKPolygonView被窃听。 我想是因为CGRectContainsPoint犯规正确计算领域,因为它不是一个矩形,它是一个多边形。

有什么其他的方法也有做到这一点? 我试图CGPathContainsPoint但我得到更坏的结果。

Answer 1:

由于@Ana·卡列尼娜,这指出了正确的方式,你这是怎么得的姿态转换,这样的方法CGPathContainsPoint “作品的权利。

- (void)mapTapped:(UITapGestureRecognizer *)recognizer{

MKMapView *mapView = (MKMapView *)recognizer.view;

MKPolygonView *tappedOverlay = nil;
int i = 0;
for (id<MKOverlay> overlay in mapView.overlays)
{
    MKPolygonView *view = (MKPolygonView *)[mapView viewForOverlay:overlay];

    if (view){
        CGPoint touchPoint = [recognizer locationInView:mapView];
        CLLocationCoordinate2D touchMapCoordinate =
        [mapView convertPoint:touchPoint toCoordinateFromView:mapView];

        MKMapPoint mapPoint = MKMapPointForCoordinate(touchMapCoordinate);

        CGPoint polygonViewPoint = [view pointForMapPoint:mapPoint];
        if(CGPathContainsPoint(view.path, NULL, polygonViewPoint, NO)){
            tappedOverlay = view;
            tappedOverlay.tag = i;
            break;
        }
    }
    i++;
}

if([[tappedOverlay fillColor] isEqual:[[UIColor cyanColor] colorWithAlphaComponent:0.2]]){
    [listOverlays addObject:tappedOverlay];
    tappedOverlay.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
}
else{
    [listOverlays removeObject:tappedOverlay];
    tappedOverlay.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
}
//tappedOverlay.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];

}


文章来源: Is CGPoint in MKPolygonView?
标签: ios mkoverlay