我怎么知道CGPoint数组是否接近另一个CGPoint数组?

时间:2013-11-13 19:25:34

标签: ios ios6 nsmutablearray uibezierpath cgpoint

我有一个由CGPoint数组组成的UIBezierPath使用addCurveToPoint函数,这里有一些代码:

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

当在视图上识别到触摸时,我得到了触摸点,我需要将其与UIBezierPath的GCPoint进行比较,并说明它是否在UIBezierPath内部。我使用的是函数containsPoint,但它不起作用,这是我的代码:

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

最后,我需要获取所有触摸点的集合并说出触摸点是否在UIBezierPath(或大多数)内部并添加边距错误,因此触摸不需要如此精确,我怎么能说UIBezierPath有多远呢?

1 个答案:

答案 0 :(得分:2)

使用CGPathCreateCopyByStrokingPath创建路径的粗线条版本(例如,lineWidth设置为22)。然后测试该点是否位于该描边路径或原始路径中。

Ole Begemann: CGPath Hit Testing

相关问题