的曲线形状图层绘制边框

问题描述:

我已经看到了这一点How to get a border on UIBezierPath的曲线形状图层绘制边框

我希望借助曲线边框

我画曲线与下面的方法

func drawCurve(from startPoint: CGPoint, to endPoint: CGPoint, controlPoint: CGPoint) { 

    var bezierPath = UIBezierPath() 
    bezierPath.move(to: startPoint) 
    //  bezierPath.addQuadCurve(to: endPoint, controlPoint: CGPoint 
    bezierPath.addLine(to: controlPoint) 
    bezierPath.addLine(to: endPoint); 


    bezierPath = bezierPath.bezierCardinal(withTension: 2.06) 
    curveSize = bezierPath.bounds 
    let strokeColor = UIColor.white 
    if curveLayer != nil { 
     curveLayer?.removeFromSuperlayer() 
     curveLayer = nil 
    } 
    curveLayer = CAShapeLayer() 
    curveLayer?.lineWidth = 1.0/self.zoomScale 
    curveLayer?.fillColor = UIColor.clear.cgColor 
    curveLayer?.path = bezierPath.cgPath 
    curveLayer?.strokeColor = strokeColor.cgColor 
    viewBase.layer.addSublayer(curveLayer!) 
} 

我已经尽量把

curveLayer?.borderWidth = 1.0 
    curveLayer?.borderColor = UIColor.yellow.cgColor 

但它没有画出边框(框内)

enter image description here

随着阿布舍克的想法,我有画边框的外形与此代码

let bazierPath2 = UIBezierPath.init(rect: curveSize) 


curveLayerForGingivalLine = CAShapeLayer() 
curveLayerForGingivalLine?.zPosition = 0.0 
curveLayerForGingivalLine?.strokeColor = UIColor.red.cgColor 
curveLayerForGingivalLine?.lineWidth = 1.0/self.zoomScale 
curveLayerForGingivalLine?.fillColor = UIColor.clear.cgColor 

let lineShapeBorder = CAShapeLayer() 

curveLayerForGingivalLine?.addSublayer(lineShapeBorder) 

lineShapeBorder.lineWidth = 1.0/self.zoomScale 
lineShapeBorder.fillColor = UIColor.clear.cgColor 
lineShapeBorder.path = bezierPath.cgPath 
lineShapeBorder.strokeColor = strokeColor.cgColor 
curveLayerForGingivalLine?.path = bazierPath2.cgPath 
viewBase.layer.addSublayer(curveLayerForGingivalLine!) 

试试下面代码 -

lineShapeBorder = CAShapeLayer() 
lineShapeBorder.zPosition = 0.0 
lineShapeBorder.strokeColor = UIColor.blue.cgColor 
lineShapeBorder.lineWidth = 25 
lineShapeBorder.lineCap = kCALineCapRound 
lineShapeBorder.lineJoin = kCALineJoinRound 
lineShapeFill = CAShapeLayer() 
lineShapeBorder.addSublayer(lineShapeFill) 
lineShapeFill.zPosition = 0.0 
lineShapeFill.strokeColor = UIColor.green.cgColor 
lineShapeFill.lineWidth = 20.0 
lineShapeFill.lineCap = kCALineCapRound 
lineShapeFill.lineJoin = kCALineJoinRound 
    // ... 
var path = UIBezierPath() 
path.move(to: lineStart) 
path.addLine(to: lineEnd) 
lineShapeFill.path = path.cgPath 
lineShapeBorder.path = lineShapeFill.path 

希望它能帮助!

+0

你能解释一下PLS代码? –

+0

我们创建了线宽为25的lineShapeBorder,因此它作为边框,并在lineShapeBorder中添加了线宽为20的lineShapeFill,因此它充当主层或内层。 –

+0

这里我的'curveLayer'会是'lineShapeFill'? –