Drawing a line with Bezierpath using CAShapeLayer object
Asked Answered
U

2

8

I am making an image editor which can create different shapes objects like circle, triangle and square which can also be updated or removed. So I have used CAShapeLayer for creating shapes objects.

Now I also want to draw a line on image which can also be updated or removed so I have used bezierpath and CAShapeLayer to create the line, it is working fine. BUT now the problem is that when I want to select any existing line it can be selected any where close to line tool because CAShapeLayer also set the fill region which will be a straight line from start point to end point.

My question is that how can I create line with no fill region using CAShapeLayer.

Here is my code for creating line:

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;

Any idea on this will be really appreciated.

Unscreened answered 2/10, 2013 at 12:0 Comment(5)
It works. Probably you forgot to add layer [self.view.layer addSublayer:line];Lessee
I did add this line too, it creates the line fine but it also fill region from start point to end point and consider it as a layer part. So the problem is that whenever I try to tap near line line layer get selected.Unscreened
Should you be using [linePath addLineToPoint:point2]?Ilonailonka
point1, point2 , point3 are the three arbitrary pointsUnscreened
if you got the solution then share it so it will helpful to other person.Conferva
A
5

Can you try this. Its work for me

    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];
Altdorfer answered 4/12, 2014 at 11:6 Comment(3)
will definitely check it and let you know.Unscreened
are you not missing [linePath stroke]?Interlaminate
You don't need [linePath stroke] here, and in fact, you'll probably get context errors if you do (unless you manually start a context or use this in drawRect) - calling stroke uses Core Graphics, but right here we want to use Core Animation.Elaterite
H
0

I understand you I experienced this issue as well try this:

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... 
}
Hogue answered 27/7, 2016 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.