2013-10-02 15 views
8

我正在制作一个图像编辑器,它可以创建不同形状的对象,如圆形,三角形和方形,这些对象也可以更新或删除。所以我用CAShapeLayer来创建形状对象。使用CAShapeLayer对象绘制与Bezierpath的线

现在我也想在图像上画一条线,也可以更新或删除,所以我用了bezierpath和CAShapeLayer来创建线条,它工作的很好。但现在的问题是,当我想选择任何现有的线时,可以在靠近线工具的地方选择任何线,因为CAShapeLayer也设置了从起点到终点的直线的填充区域。

我的问题是,如何使用CAShapeLayer创建没有填充区域的行。

这里是我创建一行代码:

CAShapeLayer *line = [CAShapeLayer layer]; 
// Using bezierpath to make line 
UIBezierPath *linePath=[UIBezierPath bezierPath]; 

// Creating L with line 

[linePath moveToPoint:point1]; 
[linePath addToPoint:point2]; 
[linePath addToPoint:point3]; 
line.path=linePath.CGPath; 


// Configure the appearence of the line 
line.fillColor = Nil; 
line.opacity = 1.0; 
line.strokeColor = [UIColor whiteColor].CGColor; 

对此有何想法会非常感激。

+0

它的工作原理。可能你忘了添加图层'''[self.view.layer addSublayer:line];''' – Shmidt

+0

我也添加了这一行,它创建的线条很好,但它也填充区域从起点到终点并考虑它作为图层的一部分。所以问题在于,每当我尝试在线路层附近点击时都会被选中。 –

+0

你应该使用'[linePath addLineToPoint:point2]'? –

回答

3

你可以试试这个。它的工作对我来说

CAShapeLayer *line = [CAShapeLayer layer]; 
    UIBezierPath *linePath=[UIBezierPath bezierPath]; 
    [linePath moveToPoint:CGPointMake(startx, starty)]; 
    [linePath addLineToPoint:CGPointMake(endx, endy)]; 
    line.lineWidth = 10.0; 
    line.path=linePath.CGPath; 
    line.fillColor = shapecolor.CGColor; 
    line.strokeColor = shapecolor.CGColor; 
    [[self.view layer] addSublayer:line]; 
+0

一定会检查它并让你知道。 –

+0

你是不是缺少[linePath stroke]? –

+0

这里你不需要'[linePath stroke]',事实上,如果你这样做的话你可能会得到上下文错误(除非你手动启动一个上下文或者在'drawRect'中使用它) - 调用'stroke'使用Core图形,但在这里我们想使用Core Animation。 – DMan

0

我理解你,我遇到这个问题,也是试试这个:

GPathRef linePathRef = linePath.CGPath 
linePathRef = CGPathCreateCopyByStrokingPath(linePathRef, NULL, line.lineWidth, kCGLineCapRound, kCGLineJoinRound, 1); 
BOOL pathContainsPoint = CGPathContainsPoint(linePathRef, NULL, touchLocation, NO); 

if(pathContainsPoint){ 
    //Do something with the cashapelayer line... 
}else{ 
    //Do something here if needed... 
}