在iOS8中检测并忽略绘制图像不透明部分的触摸 我在我的地图上有一个叠加图像(.pNG),中间有一个透明的位元,并且有彩色的边,所以用户只能集中在中间部分。然而,如果你改变中间部分的形状,在某些侧面可以看到相当多的部分

在iOS8中检测并忽略绘制图像不透明部分的触摸 我在我的地图上有一个叠加图像(.pNG),中间有一个透明的位元,并且有彩色的边,所以用户只能集中在中间部分。然而,如果你改变中间部分的形状,在某些侧面可以看到相当多的部分,ios8,mkmapview,mkoverlay,Ios8,Mkmapview,Mkoverlay,我试图检测覆盖视图上的点击,以便忽略它,只接受指定区域中的触摸 我在Ray Wenderlich的网站上遵循以下tut添加覆盖层: 图像覆盖的绘制方式如下所示: @implementation PVParkOverlayView - (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage { self = [super initWithOverlay:over

我试图检测覆盖视图上的点击,以便忽略它,只接受指定区域中的触摸

我在Ray Wenderlich的网站上遵循以下tut添加覆盖层:

图像覆盖的绘制方式如下所示:

@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);

} 
@implementation PVParkOverlayView
-(instancetype)initWithOverlay:(id)overlayImage:(UIImage*)overlayImage{
self=[super initWithOverlay:overlay];
如果(自我){
_叠加图像=叠加图像;
}
回归自我;
}
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
CGImageRef imageReference=self.overlayImage.CGImage;
//UIImage*imageTest=\u叠加图像;
MKMapRect theMapRect=self.overlay.boundingMapRect;
cRect theRect=[自校正:TheMarect];
//定向测试
//CGContextRotateCTM(上下文,0);
CGContextScaleCTM(上下文,1.0,-1.0);
CgContextTranslateCm(上下文,0.0,-theRect.size.height);
CGContextDrawImage(上下文、上下文、图像参考);
} 
我的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);
         */  
    }   
}
-(void)手势测试:(UIGestureRecognitor*)手势识别器
{
CGPoint tapPoint=[手势识别器位置视图:self.mapView];
CLLocationCoordinate2D tapCoord=[self.mapView转换点:tapPoint to CoordinateFromView:self.mapView];
MKMapPoint mapPoint=MKMapPointForCoordinate(tapCoord);
CGPoint mapPointAsCGP=CGPointMake(mapPoint.x,mapPoint.y);
用于(self.mapView.overlays中的id覆盖){
if([overlay IsKindof类:[PVParkOverlay类]]){
NSLog(@“存在覆盖”);
/*
MKPolygon*多边形=(MKPolygon*)覆盖;
CGMutablePathRef mpr=CGPathCreateMutable();
MKMapPoint*polygonPoints=polygon.points;
对于(int p=0;p
我知道叠加在那里,但由于它是一个绘制的图像,我找不到一种方法将其转换为多边形点来使用此代码(如果可能的话)

我还可以用其他方法吗

我还发现了以下示例代码,但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]);
}
-(void)mapTapped:(UITapGestureRecognitor*)识别器
{
MKMapView*mapView=(MKMapView*)识别器.view;
id tappedOverlay=零;
用于(mapView.overlays中的id覆盖)
{
MKOverlayView*视图=[mapView视图ForOverlay:overlay];
如果(视图)
{
//获取地图视图坐标系中的图幅矩形
CGRect VIEWFRAMENMPVIEW=[view.superview CONVERTRCT:view.frame TO view:mapView];
//获取mapView坐标系中的接触点
CGPoint point=[识别器位置视图:地图视图];
//检查触摸是否在视图范围内
if(CGRectContainsPoint(ViewFrameinMPView,point))
{
tappedOverlay=覆盖层;
打破
}
}
}
NSLog(@“点击视图:%@,[mapView viewForOverlay:tappedOverlay]);
}