Swift - 如何在 CAShapeLayer 或 UIBezierPath(ovalInRect:ovalRect) 上获得圆顶

Mar*_*nto 7 rounded-corners cashapelayer ios uibezierpath swift

我在我的类中有这两个函数,它们扩展了 CAShapeLayer,但是每次我绘制路径时都无法得到带有圆形端盖的弧。它看起来总是这样的图片:

在此处输入图片说明

我已经尝试使用 kCALineCapRound 没有成功。有任何想法吗?

self.lineShape.lineCap = kCALineCapRound;

self.lineShape.lineJoin = kCALineJoinRound;

  private func mask() {
    let maskLayer = CAShapeLayer()
    maskLayer.bounds = self.bounds
    let ovalRect = self.hollowRect
    let path =  UIBezierPath(ovalInRect: ovalRect)
    path.appendPath(UIBezierPath(rect: maskLayer.bounds))
    maskLayer.path = path.CGPath
    maskLayer.lineShape.lineCap = kCALineCapRound;
    maskLayer.lineShape.lineJoin = kCALineJoinRound;
    maskLayer.position = self.currentCenter
    maskLayer.fillRule = kCAFillRuleEvenOdd
    self.mask = maskLayer
  }

  private func drawTrack(ctx: CGContext) {
    let adjustDegree = Math.adjustDegree(self.setting.startAngle, degree: self.degree)
    let centerX = self.currentCenter.x
    let centerY = self.currentCenter.y
    let radius = min(centerX, centerY)
    CGContextSetFillColorWithColor(ctx, self.setting.trackingColor.CGColor)
    CGContextSetLineCap(ctx, CGLineCap.Round)
    CGContextBeginPath(ctx)
    CGContextMoveToPoint(ctx, centerX, centerY)
    CGContextAddArc(ctx, centerX, centerY, radius,
      CGFloat(Math.degreesToRadians(self.setting.startAngle)),
      CGFloat(Math.degreesToRadians(adjustDegree)), 0)
    CGContextClosePath(ctx);
    CGContextFillPath(ctx);
  }
Run Code Online (Sandbox Code Playgroud)

mat*_*att 11

如果这是一个形状图层,您只需说myShapeLayer.lineCap = "round". (我对你的蒙版和绘图代码感到困惑,这与形状图层无关,所以我不知道它应该如何适应。)

这是一个带有圆形路径的形状图层:

在此处输入图片说明

  • 您真的不应该使用定义的常量(在本例中为 kCALineJoinRound)而不是文字字符串“round”吗? (7认同)

小智 5

斯威夫特 4 版本:

myShapeLayer.lineCap = .round
Run Code Online (Sandbox Code Playgroud)

有关线路上限值的文档可以在此处找到