简体   繁体   English

Swift 3,用特殊的lineCap画圆

[英]Swift 3, draw circle with special lineCap

Currently I'm using CAShapeLayer and UIBezierPath to draw a line for avatar. 目前,我正在使用CAShapeLayer和UIBezierPath绘制头像线。 It's Ok for the normal one with lineCap = kCALineCapRound . 正常情况下,使用lineCap = kCALineCapRound即可 BUT I've been STRUGGLING to draw line with START and END lineCap like image below: 但是我一直在努力用STARTEND lineCap画线,如下图所示:

在此处输入图片说明

You can't draw this circle with a shape layer, because a shape layer supports only one single stroke color. 您无法使用形状图层绘制此圆形,因为形状图层仅支持一种笔触颜色。 You should write your own layer class where you should draw three separate circle segments with kCALineCapRound : 您应该编写自己的图层类,在其中应使用kCALineCapRound绘制三个单独的圆形线段:

  1. A full yellow circle. 一个完整的黄色圆圈。
  2. Draw the blue segment over the yellow circle. 在黄色圆圈上绘制蓝色线段。 It is visible and you can see the rounded ends. 它是可见的,您可以看到圆形的末端。
  3. Draw a segment of the yellow circle, say 5 degrees of the top. 画出黄色圆圈的一部分,例如与顶部的5度角。 Then you can also see the rounded end of the yellow line. 然后,您还可以看到黄线的圆形末端。

You should use CGPath and CGContext instead of UIBezierPath . 您应该使用CGPathCGContext而不是UIBezierPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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