在iOS8中检测并忽略MKOverlay绘制的图像的非透明部分上的触摸

时间:2014-12-11 22:22:23

标签: ios8 mkmapview mkoverlay

我的地图上有一个叠加图像(.png),由中间的透明位和彩色边组成,因此用户只能对焦于中间部分。然而,对于那个中间位的形状,在某些方面可以看到相当多的一些。

我试图在OverlayView上检测到一个水龙头,所以我可以忽略它,只接受指定区域的触摸。

我在Ray Wenderlich的网站上跟随以下内容添加了叠加层:

图像叠加层的绘制方式如下:

@implementation PVParkOverlayView

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
self = [super initWithOverlay:overlay];
if (self) {
    _overlayImage = overlayImage;
}

return self;
}

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGImageRef imageReference = self.overlayImage.CGImage;

//UIImage *imageTest = _overlayImage;

MKMapRect theMapRect = self.overlay.boundingMapRect;
CGRect theRect = [self rectForMapRect:theMapRect];

//orientation testing
//CGContextRotateCTM(context, 0);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);

} 

我的mapview上有一个手势识别器,我正试图检测那里的水龙头:

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{

CGPoint tapPoint = [gestureRecognizer locationInView:self.mapView];

CLLocationCoordinate2D tapCoord = [self.mapView convertPoint:tapPoint toCoordinateFromView:self.mapView];
MKMapPoint mapPoint = MKMapPointForCoordinate(tapCoord);
CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);

for (id<MKOverlay> overlay in self.mapView.overlays) {
    if([overlay isKindOfClass:[PVParkOverlay class]]){

       NSLog(@"overlay is present");

        /*
        MKPolygon *polygon = (MKPolygon*) overlay;

        CGMutablePathRef mpr = CGPathCreateMutable();

        MKMapPoint *polygonPoints = polygon.points;

        for (int p=0; p < polygon.pointCount; p++){
            MKMapPoint mp = polygonPoints[p];
            if (p == 0)
                CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
            else
                CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
        }

        if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE)){
            // ... found it!
            NSLog(@"I've found it!");
        }

        //CGPathRelease(mpr);
         */  
    }   
}

我知道叠加层就在那里,但由于它是一个绘制的图像,我无法找到将其转换为多边形点以使用此代码的方法(如果可能的话)。

我可以使用其他任何方法吗?

我还发现了以下示例代码,但不推荐使用viewForOverlay方法:

- (void)mapTapped:(UITapGestureRecognizer *)recognizer
{
  MKMapView *mapView = (MKMapView *)recognizer.view;
  id<MKOverlay> tappedOverlay = nil;
  for (id<MKOverlay> overlay in mapView.overlays)
  {
    MKOverlayView *view = [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 = overlay;
    break;
  }
}
}
NSLog(@"Tapped view: %@", [mapView viewForOverlay:tappedOverlay]);
}

0 个答案:

没有答案