迅速绘制视图背景的弧线

时间:2018-08-14 21:24:12

标签: ios swift uiview draw uibezierpath

我想在这样的视图的底部绘制一个弧形:

enter image description here

背景图案是另一回事,但是我认为可以通过uibezierpath之类的东西来完成吗?我没有太多经验,任何入门点吗?谢谢

1 个答案:

答案 0 :(得分:4)

将此自定义类分配给IB中的视图

class CurveView:UIView {

    var once = true

    override func layoutSubviews() {
        super.layoutSubviews()

        if once {

            let bb = UIBezierPath()

            bb.move(to: CGPoint(x: 0, y: self.frame.height))

            // the offset here is 40 you can play with it to increase / decrease the curve height

            bb.addQuadCurve(to: CGPoint(x: self.frame.width, y: self.frame.height), controlPoint: CGPoint(x: self.frame.width / 2 , y: self.frame.height + 40 ))

            bb.close()

            let l = CAShapeLayer()

            l.path = bb.cgPath

            l.fillColor =  self.backgroundColor!.cgColor

            self.layer.insertSublayer(l,at:0)

            once = false
        }

    }

}

//

enter image description here