Pregunta

I have a UIBezierPath made of an array of CGPoint´s using the function addCurveToPoint, here is a little of the code:

UIBezierPath *path = [[UIBezierPath alloc] init];
[path setLineWidth:10];
[path moveToPoint:pts[0]];
for(int i = 0; i<[array count]; i++) {
     [path addCurveToPoint:[array objectAtIndex:i] controlPoint1:pts[1] controlPoint2:pts[2]];
}

When a touch is recognized on the view, i get the point of the touch and i need to compare it to the GCPoint´s of the UIBezierPath and say if it is inside of the UIBezierPath or not. What i have used is the function containsPoint but it didn´t work, here is my code:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    CGPoint point = CGPointMake(p.x, p.y);
    NSLog(@"actual point x %f y %f", point.x, point.y);

    if ([path containsPoint:point]) {
        NSLog(@"it contains the point");
    }
}

At the end, i need to get all the set of the touched points and say if the touched points are inside of the UIBezierPath(or most of them) and add a margin error so the touch doesnt need to be so precisely, also how can i say how far is the point of the UIBezierPath?

¿Fue útil?

Solución

Use CGPathCreateCopyByStrokingPath to create a thickly stroked version of your path (for example, with lineWidth set to 22). Then test whether the point is inside either of that stroked path or your original path.

Ole Begemann: CGPath Hit Testing

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top