0

I have a layer for a camera you can see in image 1, I need a rounded corner like image 2. How I can round it.

Image 1: enter image description here Image 2: enter image description here

    if cornerRadius > cornerLength { cornerRadius = cornerLength }
    if cornerLength > maskContainer.width / 2 { cornerLength = maskContainer.width / 2 }

    let upperLeftPoint = CGPoint(x: maskContainer.minX, y: maskContainer.minY)

    let upperLeftCorner = UIBezierPath()
    upperLeftCorner.move(to: upperLeftPoint.offsetBy(dx: 0, dy: cornerLength))
    upperLeftCorner.addArc(withCenter: upperLeftPoint.offsetBy(dx: cornerRadius, dy: cornerRadius),
                           radius: cornerRadius, startAngle: .pi, endAngle: 3 * .pi / 2, clockwise: true)
    upperLeftCorner.addLine(to: upperLeftPoint.offsetBy(dx: cornerLength, dy: 0))

    let combinedPath = CGMutablePath()
    combinedPath.addPath(upperLeftCorner.cgPath)
Valeriy
  • 723
  • 6
  • 17

1 Answers1

3
let cgPath = CGMutablePath()
cgPath.move(to: bottomLeftPoint)
cgPath.addArc(tangent1End: topLeftPoint, tangent2End: topRightPoint, radius: radius)
cgPath.addLine(to: topRightPoint)
let bezierPath = UIBezierPath(cgPath: cgPath)

enter image description here

Valeriy
  • 723
  • 6
  • 17
klememi
  • 96
  • 1
  • 6
  • The `addArc(tangent1End:tangent2End:radius)` function is perfect for this, and very easy to use. (Once you figure it out. It's not very well documented.) I was going to suggest that, but you beat me to it. Voted. – Duncan C Sep 01 '21 at 00:18