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

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

现在我也想绘制一个图像,也可以更新或删除线,所以我用bezierpath和CAShapeLayer来创build线,它工作正常。 但现在的问题是,当我想要select任何现有的线可以select任何地方接近线工具,因为CAShapeLayer也设置填充区域,这将是一条从起点到终点的直线。

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

这是我创build线的代码:

 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; 

任何想法,这将非常感激。

你可以试试这个吗? 它为我工作

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

我了解你,我也遇到过这个问题,试试这个:

 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... }