使用CAShapeLayer和UIBezierPath在UIView上画线。
// 创建出CAShapeLayer
CAShapeLayer *linelayer = [CAShapeLayer layer];
linelayer.frame = CGRectMake(0.0, 0.0, self.scrollView.contentSize.width, self.scrollView.height);;
linelayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor
// 设置线条的宽度和颜色
linelayer.lineWidth = 1.0f;
linelayer.strokeColor = self.xLine.backgroundColor.CGColor;
//
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
// 设置线段的起始位置
[bezierPath moveToPoint:CGPointMake(10.0, 10.0)];
// 添加点
[bezierPath addLineToPoint:CGPointMake(0.0, 0.0)];
[bezierPath addLineToPoint:CGPointMake(100.0, 100.0)];
[bezierPath addLineToPoint:CGPointMake(130.0, 60.0)];
[bezierPath addLineToPoint:CGPointMake(180.0, 20.0)];
//
linelayer.path = bezierPath.CGPath;
[self.view.layer addSublayer:linelayer];
效果.png
网友评论