2015-07-11 40 views
0

我有一个复杂的路径,几个贝塞尔段连接在一起。此路径是动态的,用户可以添加和删除此路径内的点。用于点检测的宽贝塞尔路径

当我画的路径,我救UIBezierPath副本,就像这样:

CGContextBeginPath(context); 
for (NSUInteger i = 0; i < _points.count - 1; i++) 
{ 
    // ... 
    CGContextAddCurveToPoint(context, cp1x, cp1y, cp2x, cp2y, endX, endY); 
} 
_path = [UIBezierPath bezierPathWithCGPath:CGContextCopyPath(context)]; 
CGContextStrokePath(context); 

我创建长按手势新点:

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer 
{ 
    // ... 
    CGPoint point = [recognizer locationInView:self]; 
    if ([_path containsPoint:point]) 
    { 
     // process point 
    } 
    // ... 
} 

但是,这需要用户挖掘非常接近路径。我希望在我认为任何抽头有效的路径周围有更广阔的区域(如线宽)。

如何配置UIBezierPath以允许更大的区域?我想要它控制这个区域可能有多宽。

回答

0

以下是可能的解决方案:

CGPathRef pathCopy = CGContextCopyPath(context); 
// 16 is the width of path where we want to have hit test 
CGPathRef tapPath = CGPathCreateCopyByStrokingPath(pathCopy, NULL, 16, kCGLineCapButt, kCGLineJoinMiter, 0.6); 
// this path will be used for hit test 
_linePath = [UIBezierPath bezierPathWithCGPath:tapPath]; 
[_linePath closePath]; 

现在的地方,我们需要它:

CGPoint point = [recognizer locationInView:self]; 
if ([_linePath containsPoint:point]) 
{ 
    // work with point 
}