在曲线形状图层上绘制边框

时间:2017-07-27 12:52:04

标签: ios swift3 uibezierpath

我已经看过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

2 个答案:

答案 0 :(得分:1)

尝试以下代码 -

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

希望它有所帮助!

答案 1 :(得分:0)

凭借Abhishek的想法,我用这个代码绘制了Border On Shape

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!)