0

我已经设置了一个CGPath这样的。

- (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);
}

现在,当我单击视图时。我想检查此点击是否在路径内。

-(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];
}

现在奇怪的是。当我点击我得到这个日志

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

那是100%确定的路径。但我没有得到"Clicked in the path". 有人可以帮我吗?

谢谢!

4

1 回答 1

0

用这个

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

在其中调用一个函数在循环中创建一个路径并在路径上调用上述方法首先创建一个路径。

于 2013-06-14T09:52:12.417 回答