我已经设置了这样的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"
的日志。有人可以帮我吗?
谢谢!
答案 0 :(得分:0)
使用此
CGContextPathContainsPoint(context, point, mode) //the names of the parameters are explanatory use mode=kCGPathStroke;
调用里面的函数在循环中创建一个路径并在路径上调用上面的方法首先创建一个路径。