2016-07-31 28 views
2

我有這2個功能波紋管在我的課延伸CAShapeLayer,但我不能得到與圓形端的圓弧帽每次我繪製的路徑時。它看起來總是像這樣的畫面:斯威夫特 - 如何獲得圓形帽上CAShapeLayer或UIBezierPath(ovalInRect:ovalRect)

enter image description here

我已經使用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); 
    } 

回答

2

如果這是一個形狀圖層,那麼您只需說myShapeLayer.lineCap = "round"。 (我被你的面具迷惑了,並且畫出了與形狀層無關的代碼,所以我不明白它應該如何適應。)

這是一個帶有圓形路徑的形狀圖層:

enter image description here

+6

你不應該真正使用定義的常量(kCALineJoinRound,在這種情況下),而不是文本字符串「圓」? –

+0

@DuncanC我做了什麼,頭告訴我這樣做。有效。我停下了。 – matt

+0

你介意張貼代碼在這裏,紅色的圓圈? –