【问题标题】:How to draw a line between two points objective-C如何在两点之间画一条线objective-C
【发布时间】:2019-09-06 04:18:49
【问题描述】:

当试图在两点(imageViews)之间画一条线时,这条线是在另一个位置生成的。

这是我的代码:

- (void)drawLine:(UIView*)currentPoint endPoint:(UIImageView*)lastPoint{

    UIBezierPath* path = [UIBezierPath bezierPath];
    [path moveToPoint:currentPoint.center];
    [path addLineToPoint:lastPoint.center];


    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];
    shapeLayer.lineWidth = 1.0;
    [self.DrawView.layer addSublayer:shapeLayer];

}

【问题讨论】:

  • 一切看起来都不错。你能分享/检查你传递给画线函数的CGPoints的值吗

标签: ios objective-c


【解决方案1】:

请确保您的currentPointlastPointself.DrawView 中。

您可以在currentPoint.superview.layer 中添加一行,如下所示

- (void)drawLine: (UIView*)currentPoint endPoint:(UIImageView*)lastPoint{
    UIBezierPath* path = [UIBezierPath bezierPath];
    [path moveToPoint:currentPoint.center];
    [path addLineToPoint:lastPoint.center];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];
    shapeLayer.lineWidth = 1.0;
    [currentPoint.superview.layer addSublayer:shapeLayer];
}

【讨论】:

    【解决方案2】:

    看起来你需要为你的 CAShapeLayer 提供一个框架。

    shapeLayer.frame = CGRectMake(0, 0, 100, 100)

    类似的东西

    【讨论】: