iOS - 利用 UIBezierPath 绘制圆弧

iOS - 利用 UIBezierPath 绘制圆弧

API

UIBezierPath 绘制圆弧主要利用以下方法:

open func addArc(withCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)

方法中各参数含义:

  • center:圆心
  • radius:半径
  • startAngle:开始弧度
  • endAngle:结束弧度
  • clockwise:绘制方向,YES 为顺时针,NO 为逆时针

20210219002611367

要绘制这样一个圆弧

20210221120752180

let radius = 40.0
        let startPoint = CGPointMake(50, 200)
        let endPoint = CGPointMake(150, 200)
        let centerPoint = CGPointMake(150 + radius, 200)

        let path = UIBezierPath()
        //逆时针
        path.addArc(withCenter: centerPoint, radius: radius, startAngle: .pi, endAngle: 1.5 * .pi, clockwise: false)
        //顺时针
        path.addArc(withCenter: centerPoint, radius: radius, startAngle: 1.5 * .pi, endAngle: .pi, clockwise: true)

两个点的度数分别是pi和1.5pi

path画好之后添加到CAShapeLayer上

let tempV = CAShapeLayer()
tempV.strokeColor = UIColor.plw_hexColor("#58CE72").cgColor
        tempV.lineWidth = lineWidth
        tempV.fillColor = UIColor.clear.cgColor
        tempV.lineCap = .round
        tempV.path = path.cgPath

并设置一些属性,线条的颜色,线条的形状,宽度,内部的填充颜色等等

self.view.layer.addSublayer(tempV)

最后将此layer添加到view的layer上

posted @ 2023-04-05 14:03  R1cardo  阅读(617)  评论(0编辑  收藏  举报