使用CALayer绘制一条直线

15

我试图使用CALayer在两个点之间画一条线。以下是我的代码:

//positions a CALayer to be a line between a parent node and its subnodes.

-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB{
    NSLog([NSString stringWithFormat:@"Coordinates: \n Ax: %f Ay: %f Bx: %f By: %f", pointA.x,pointA.y,pointB.x,pointB.y]);

    //find the length of the line:
    CGFloat length = sqrt((pointA.x - pointB.x) * (pointA.x - pointB.x) + (pointA.y -     pointB.y) * (pointA.y - pointB.y));
    layer.frame = CGRectMake(0, 0, 1, length);

    //calculate and set the layer's center:
    CGPoint center = CGPointMake((pointA.x+pointB.x)/2, (pointA.y+pointB.y)/2);
    layer.position = center;

    //calculate the angle of the line and set the layer's transform to match it.
    CGFloat angle = atan2f(pointB.y - pointA.y, pointB.x - pointA.x);
    layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
}

我知道长度计算正确,而且我相当确定中心点也是正确的。运行程序后,显示的线条长度正确,穿过两点之间的中心点,但旋转方向不正确。起初我认为线条围绕着错误的锚点旋转,所以我尝试了这段代码:layer.anchorPoint = center;,但这段代码没有在屏幕上显示任何线条。我做错了什么?


1
锚点位于单位坐标空间中(x和y都从0到1)。这意味着无论图层的大小和纵横比如何,居中的锚点都是(0.5,0.5)。 - David Rönnqvist
好的,所以执行layer.anchorPoint = center;会将锚点设置到很远的地方,旋转就会超出屏幕的范围...这解释了为什么它没有显示。不过,你有任何想法是什么问题吗? - 67cherries
3个回答

33

试试这个...

-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB
{
    CAShapeLayer *line = [CAShapeLayer layer];
    UIBezierPath *linePath=[UIBezierPath bezierPath];
    [linePath moveToPoint: pointA];
    [linePath addLineToPoint:pointB];
    line.path=linePath.CGPath;
    line.fillColor = nil;
    line.opacity = 1.0;
    line.strokeColor = [UIColor redColor].CGColor;
    [layer addSublayer:line];
}

1
是的。不要创建一个倾斜的CALayer来表示一条线。使用CAShapeLayer,并设置其路径。 - jrturton

21

这是基于Rajesh Choudhary的回答的Swift版本:

Swift 2

func drawLine(onLayer layer: CALayer, fromPoint start: CGPoint, toPoint end: CGPoint) {
    let line = CAShapeLayer()
    let linePath = UIBezierPath()
    linePath.moveToPoint(start)
    linePath.addLineToPoint(end)
    line.path = linePath.CGPath
    line.fillColor = nil
    line.opacity = 1.0
    line.strokeColor = UIColor.redColor().CGColor
    layer.addSublayer(line)
}

Swift 3

func drawLine(onLayer layer: CALayer, fromPoint start: CGPoint, toPoint end: CGPoint) {
    let line = CAShapeLayer()
    let linePath = UIBezierPath()
    linePath.move(to: start)
    linePath.addLine(to: end)
    line.path = linePath.cgPath
    line.fillColor = nil
    line.opacity = 1.0
    line.strokeColor = UIColor.red.cgColor
    layer.addSublayer(line)
}

1
最好返回图层,让调用者自己插入它(在另一图层的上方或下方,根据调用者的需要),而不是为其插入。 - Alexander

0

这是基于Rajesh Choudhary的答案的Swift 3版本:

func drawLine(onLayer layer: CALayer, fromPoint start: CGPoint, toPoint end:CGPoint) {
        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.move(to: start)
        linePath.addLine(to: end)
        line.path = linePath.cgPath
        line.fillColor = nil
        line.opacity = 1.0
        line.strokeColor = UIColor.green.cgColor
        layer.addSublayer(line)
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接