0

I have set out a CGPath like this.

- (void)drawRect:(CGRect)rect
{
    UIImage *myImage = [UIImage imageNamed:@"mapBelgie.png"];
    CGRect imageRect = self.bounds;
    [myImage drawInRect:imageRect];
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGMutablePathRef pathLommel = CGPathCreateMutable();

    CGPathMoveToPoint(pathLommel, NULL,686,80);
    CGPathMoveToPoint(pathLommel, NULL,729,76);
    CGPathMoveToPoint(pathLommel, NULL,747,68);
    CGPathMoveToPoint(pathLommel, NULL,773,94);
    CGPathMoveToPoint(pathLommel, NULL,831,111);
    CGPathMoveToPoint(pathLommel, NULL,788,220);
    CGPathMoveToPoint(pathLommel, NULL,658,247);
    CGPathMoveToPoint(pathLommel, NULL,679,182);
    CGPathMoveToPoint(pathLommel, NULL, 637,176);
    CGPathMoveToPoint(pathLommel, NULL,674,148);
    CGPathMoveToPoint(pathLommel, NULL, 624,140);
    CGPathMoveToPoint(pathLommel, NULL, 700,108);
    CGPathMoveToPoint(pathLommel, NULL, 686,80);

    CGPathCloseSubpath(pathLommel);

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);

    CGContextSetLineJoin(context, kCGLineJoinMiter);
    CGContextSetLineWidth(context, 1.0f);
    CGContextAddPath(context, pathLommel);
    arrayPaths = [[NSMutableArray alloc]init];
    [arrayPaths addObject:CFBridgingRelease(pathLommel)];

    CGContextDrawPath(context, kCGPathFillStroke);
}

Now When i click on a view. I want to check if this click was inside the path.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint c = [[touches anyObject] locationInView: self];
    NSLog(@"point is X = %f and Y = %f",c.x,c.y );
    struct CGPath *pat = (__bridge struct CGPath *)([arrayPaths objectAtIndex:0]);
    CGPathRef  strokedPath = CGPathCreateCopy(pat);

    BOOL pointIsNearPath = CGPathContainsPoint(strokedPath, NULL, c, NO);
    if (pointIsNearPath){
        NSLog(@"Clicked in the path");
        region = 0;
    }
    [self.delegationListener didPressRegion:region];
}

Now the strange thing is. When I click I get this log

2013-06-14 11:42:29.372 Architab[27742:c07] point is X = 703.000000 and Y = 94.000000

That is for 100% sure in the path. But I don't geet the log of "Clicked in the path". Can anybody help me with this?

thanks!

Steaphann
  • 2,797
  • 6
  • 50
  • 109

1 Answers1

0

Use this

 CGContextPathContainsPoint(context, point, mode)  //the names of the parameters are explanatory use mode=kCGPathStroke;

call a function inside it make a path in loop and call the above method on the path first make a path.

amar
  • 4,285
  • 8
  • 40
  • 52