79414972

Date: 2025-02-05 13:51:25
Score: 0.5
Natty:
Report link

Here's n quick implementation of a HalfCircleView using CAShapeLayer and UIBezierPath:

class HalfCircleView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupHalfCircle()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupHalfCircle()
    }

    private func setupHalfCircle() {
        let shapeLayer = CAShapeLayer()
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: bounds.maxY))
        path.addArc(withCenter: CGPoint(x: bounds.midX, y: bounds.maxY),
                    radius: bounds.width / 2,
                    startAngle: CGFloat.pi,
                    endAngle: 0,
                    clockwise: true)
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = UIColor.blue.cgColor
        layer.addSublayer(shapeLayer)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        setupHalfCircle()
    }
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Frankie Baron