ios – 使用CAShapeLayer对象与Bezierpath绘制一条线

前端之家收集整理的这篇文章主要介绍了ios – 使用CAShapeLayer对象与Bezierpath绘制一条线前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作一个图像编辑器,可以创建不同的形状对象,如圆形,三角形和方形,也可以更新或删除.所以我使用CAShapeLayer来创建形状对象.

现在我还想在图像上绘制一条线,也可以更新或删除,所以我使用bezierpath和CAShapeLayer来创建线,它工作正常.但现在的问题是,当我想选择任何现有的线时,可以选择接近线工具的任何位置,因为CAShapeLayer还设置了从起点到终点的直线的填充区域.

我的问题是如何使用CAShapeLayer创建没有填充区域的行.

这是我创建行的代码

  1. CAShapeLayer *line = [CAShapeLayer layer];
  2. // Using bezierpath to make line
  3. UIBezierPath *linePath=[UIBezierPath bezierPath];
  4.  
  5. // Creating L with line
  6.  
  7. [linePath moveToPoint:point1];
  8. [linePath addToPoint:point2];
  9. [linePath addToPoint:point3];
  10. line.path=linePath.CGPath;
  11.  
  12.  
  13. // Configure the appearence of the line
  14. line.fillColor = Nil;
  15. line.opacity = 1.0;
  16. line.strokeColor = [UIColor whiteColor].CGColor;

对此有任何想法将非常感激.

解决方法

你能试试吗?它的工作对我来说
  1. CAShapeLayer *line = [CAShapeLayer layer];
  2. UIBezierPath *linePath=[UIBezierPath bezierPath];
  3. [linePath moveToPoint:CGPointMake(startx,starty)];
  4. [linePath addLineToPoint:CGPointMake(endx,endy)];
  5. line.lineWidth = 10.0;
  6. line.path=linePath.CGPath;
  7. line.fillColor = shapecolor.CGColor;
  8. line.strokeColor = shapecolor.CGColor;
  9. [[self.view layer] addSublayer:line];

猜你在找的iOS相关文章