使用CAShapeLayer对象使用Bezierpath绘制线条

时间:2013-10-02 12:00:52

标签: ios objective-c uibezierpath cashapelayer

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

现在我还想在图像上画一条线也可以更新或删除所以我使用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;

对此的任何想法都将非常感激。

2 个答案:

答案 0 :(得分:4)

你能试试吗?它为我工作

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

答案 1 :(得分: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... 
}
相关问题