简体   繁体   中英

How to draw a line between two points objective-C

When trying to draw a line between two points (imageViews), the line is generated in another position.

在此处输入图片说明

This is my Code:

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

}

Please make sure your currentPoint and lastPoint is in self.DrawView .

Instead of you can add line in currentPoint.superview.layer as below

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

Looks like you need to give a frame for your CAShapeLayer.

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

something like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM