UIView with UIBezierPath with reversing path

时间:2017-10-04 10:50:27

标签: ios swift3 uiview uibezierpath cashapelayer

我尝试使用UIBezierPath addCurve()方法在底部视图中提供一些模板。

预期结果

enter image description here

当前输出

enter image description here

我尝试过的是

        viewHeader.backgroundColor = headerColor
        let path = UIBezierPath()
        let cpoint1 = CGPoint(x: 80, y: 160)
        let cpoint2 = CGPoint(x: 280, y: viewHeader.Height + 50)
        path.move(to:.init(x: 0, y: viewHeader.Height + 10))
        path.addCurve(to: .init(x: viewHeader.Width, y: 205), controlPoint1: cpoint1, controlPoint2: cpoint2)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.reversing().cgPath
        shapeLayer.fillColor = headerColor?.cgColor
        shapeLayer.strokeColor =  UIColor.black.cgColor
        shapeLayer.lineWidth = 1.0
        viewHeader.layer.addSublayer(shapeLayer)

1 个答案:

答案 0 :(得分:1)

我希望它能为你效劳,享受。

func drawLine() {
    let headerColor = UIColor.red
    let path = UIBezierPath()
    let cpoint1 = CGPoint(x: viewHeader.frame.width/4, y: 160)
    let cpoint2 = CGPoint(x: viewHeader.frame.width*3/4, y: viewHeader.frame.height + 50)
    path.move(to:.init(x: 0, y: 0))
    path.addLine(to:.init(x: 0, y: viewHeader.frame.height - 50))
    path.addCurve(to: .init(x: viewHeader.frame.width, y: viewHeader.frame.height - 50), controlPoint1: cpoint1, controlPoint2: cpoint2)
    path.addLine(to: CGPoint(x: viewHeader.frame.width, y: 0))
    path.addLine(to: CGPoint(x: 0, y: 0))
    path.close()
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.reversing().cgPath
    shapeLayer.fillColor = headerColor.cgColor
    shapeLayer.strokeColor =  headerColor.cgColor
    shapeLayer.lineWidth = 1.0
    viewHeader.layer.addSublayer(shapeLayer)
}