Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to get Rounded Cap on CAShapeLayer or UIBezierPath(ovalInRect: ovalRect)

I have these 2 functions bellow in my class which extends CAShapeLayer, but I can't get an arc with rounded end caps every time I draw the path. It looks always like this picture:

enter image description here

I already tried using kCALineCapRound with no success. Any ideas?

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);
  }
like image 586
Marco Nascimento Avatar asked Jul 31 '16 23:07

Marco Nascimento


1 Answers

If this is a shape layer, you just say myShapeLayer.lineCap = "round". (I'm mystified by your mask and draw code, which has nothing to do with a shape layer, so I don't see how that's supposed to fit in.)

This is a shape layer with a circular path:

enter image description here

like image 84
matt Avatar answered Oct 05 '22 22:10

matt