iOS 贝塞尔曲线基本使用

楼主之前接到了一个需求,其中要求tableviewCell中有虚线分割线,于是顺便学习了一下贝塞尔曲线的使用。

func draw(_ rect: CGRect) 方法里,viewcontroller不可以
override func draw(_ rect: CGRect) {
}

      画虚线
UIColor.red.set()
        let verticalLinePath = UIBezierPath.init()
        verticalLinePath.lineWidth = 1.0
        let arr = [CGFloat(3),CGFloat(3)]
        verticalLinePath.setLineDash(arr, count: 2, phase: 0.0)
        verticalLinePath.move(to: CGPoint(x: 0, y: 100))
        verticalLinePath.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 100))
        verticalLinePath.stroke()
        verticalLinePath.fill()

画曲线

//        UIColor.red.set()
//        let path = UIBezierPath.init()
//        path.lineWidth = 5.0
//        path.move(to: CGPoint(x: 0, y: 0))
//        path.addLine(to: CGPoint(x: 0, y: 100))
////        path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 100))
////        path.addArc(withCenter: CGPoint(x: UIScreen.main.bounds.width / 2, y: 0), radius: UIScreen.main.bounds.width , startAngle: 3.14159 / 4, endAngle: 3.14159 / 2, clockwise: true)
//        path.addQuadCurve(to: CGPoint(x: UIScreen.main.bounds.width, y: 100), controlPoint: CGPoint(x: UIScreen.main.bounds.width / 2, y: 175))
//        path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 0))
//        path.close()
//        path.fill

画椭圆

//        UIColor.green.set()
//        let path = UIBezierPath.init(ovalIn: CGRect(x: 0, y: 0, width: 200, height: 100))
//        path.fill()
////        path.stroke()

画直线折线

//        UIColor.red.set()
//        let path = UIBezierPath.init()
//        path.move(to: CGPoint(x: 0, y: 0))
//        path.addLine(to: CGPoint(x: 100, y: 100))
//        path.addLine(to: CGPoint(x: 100, y: 200))
//        path.close()
//        path.lineCapStyle = CGLineCap.round
//        path.lineJoinStyle = CGLineJoin.round
//        path.lineWidth = 5.0
//
//        path.fill()
//        path.stroke()

画圆弧

//        UIColor.green.set()
//
//        let path = UIBezierPath.init(arcCenter: CGPoint(x: 200, y: 200), radius: 90, startAngle: 0, endAngle: 2, clockwise: true)
//        path.lineWidth = 5.0
//        path.lineJoinStyle = CGLineJoin.round
//        path.lineCapStyle = CGLineCap.round
//        path.stroke()


let shapeLayer = CAShapeLayer() shapeLayer.path = path.cgPath //存入UIBezierPath的路径
shapeLayer.fillColor = nil//设置填充色
shapeLayer.lineWidth = 2//设置路径线的宽度shapeLayer.strokeColor = UIColor.blue.cgColor //路径颜色self.view.layer.addSublayer(shapeLayer)

}

还有一些画圆角和涉及动画的贝塞尔曲线暂时没有po出来,因为代码忘记在哪个demo里了 找到了再补充

你可能感兴趣的:(iOS 贝塞尔曲线基本使用)