2015-07-11 42 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 
}