Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用CAShapeLayer对象使用Bezierpath绘制直线_Ios_Objective C_Uibezierpath_Cashapelayer - Fatal编程技术网

Ios 使用CAShapeLayer对象使用Bezierpath绘制直线

Ios 使用CAShapeLayer对象使用Bezierpath绘制直线,ios,objective-c,uibezierpath,cashapelayer,Ios,Objective C,Uibezierpath,Cashapelayer,我正在制作一个图像编辑器,它可以创建不同形状的对象,如圆形、三角形和正方形,这些对象也可以更新或删除。因此,我使用了CAShapeLayer来创建形状对象 现在我还想在图像上画一条线,它也可以被更新或删除,所以我使用bezierpath和CAShapeLayer创建了这条线,它工作得很好。但现在的问题是,当我想要选择任何现有直线时,可以选择任何靠近直线工具的位置,因为CAShapeLayer还设置了填充区域,该区域将是从起点到终点的直线 我的问题是如何使用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;

如果您对此有任何想法,我们将不胜感激。

您能试试这个吗。这是我的工作

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

它起作用了。可能您忘记添加层
[self.view.layer addSublayer:line]我也添加了这条线,它创建了线细,但它也填充从起点到终点的区域,并将其视为一个层部分。所以问题是,每当我尝试点击近线层时,线层都会被选中。您是否应该使用
[linePath addLineToPoint:point2]
?点1、点2、点3是三个任意点如果您得到解决方案,然后将其共享,这样对其他人会有帮助。我肯定会检查它并让您知道。您没有遗漏[linePath stroke]?这里不需要
[linePath stroke]
,事实上,如果这样做,您可能会遇到上下文错误(除非您手动启动上下文或在
drawRect
中使用它)-调用
stroke
使用核心图形,但在这里我们希望使用核心动画。