Trim 具有 2 个圆弧的 UIView

Trim UIView with 2 arcs

我有一个 UIView,我想 trim 它有两个圆圈,就像我 drawn(抱歉质量不好)。

我的代码:

final class TrimmedView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)

        let size = CGSize(width: 70, height: 70)
        let innerRadius: CGFloat = 366.53658283002471
        let innerBottomRadius: CGFloat = 297.88543112651564

        let path = UIBezierPath()
        path.move(to: CGPoint(x: -innerRadius + (size.width / 2), y: innerRadius))
        path.addArc(withCenter: CGPoint(x: size.width / 2, y: innerRadius), radius: innerRadius, startAngle: CGFloat.pi, endAngle: 0, clockwise: true)
        path.move(to: CGPoint(x: -innerBottomRadius + (size.width / 2), y: innerBottomRadius))
        path.addArc(withCenter: CGPoint(x: size.width / 2, y: innerBottomRadius), radius: innerBottomRadius, startAngle: 0, endAngle: CGFloat.pi, clockwise: true)

        path.close()
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.shadowPath = path.cgPath
        layer.mask = shapeLayer
    }

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

ViewController:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let view = UIView(frame: CGRect(origin: CGPoint(x: (self.view.bounds.width - 70) / 2, y: (self.view.bounds.height - 70) / 2), size: CGSize(width: 70, height: 70)))
    view.backgroundColor = .red
    self.view.addSubview(view)
    let view1 = TrimmedView(frame: view.frame)
    view1.backgroundColor = .yellow
    self.view.addSubview(view1)
}

我明白了 result。在我看来,top trimming 有效,但 bottom 无效,我不知道为什么。任何帮助,将不胜感激。谢谢。

这是一个自定义视图,可以满足您的需求。

UIBezierPath 对顶部 "convex" 弧和底部 "concave" 弧使用 QuadCurves。

它被标记为 @IBDesignable,因此您可以在设计时在 IB / Storyboard 中看到它。圆弧的 "height" 和填充颜色均设置为 @IBInspectable,因此您也可以在设计时调整这些值。

在 Storyboard 中使用它:

  • 添加一个普通的UIView
  • 将 Class 更改为 BohdanShapeView
  • 在“属性检查器”窗格中,设置弧偏移和填充颜色
  • 将背景颜色设置为普通视图(您可能会使用透明)

结果:

通过代码使用它:

let view1 = BohdanShapeView(frame: view.frame)
view1.fillColor = .systemTeal
view1.arcOffset = 10
self.view.addSubview(view1)

这里是 class:

@IBDesignable
class BohdanShapeView: UIView {

    @IBInspectable var arcOffset: CGFloat = 0.0
    @IBInspectable var fillColor: UIColor = UIColor.white

    let shapeLayer = CAShapeLayer()

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

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

    func commonInit() -> Void {
        // add the shape layer
        layer.addSublayer(shapeLayer)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // fill color for the shape
        shapeLayer.fillColor = self.fillColor.cgColor

        let width = bounds.size.width
        let height = bounds.size.height

        let bezierPath = UIBezierPath()

        // start at arcOffset below top-left
        bezierPath.move(to: CGPoint(x: 0.0, y: 0.0 + arcOffset))

        // add curve to arcOffset below top-right
        bezierPath.addQuadCurve(to: CGPoint(x: width, y: 0.0 + arcOffset), controlPoint: CGPoint(x: width * 0.5, y: 0.0 - arcOffset))

        // add line to bottom-right
        bezierPath.addLine(to: CGPoint(x: width, y: height))

        // add curve to bottom-left
        bezierPath.addQuadCurve(to: CGPoint(x: 0.0, y: height), controlPoint: CGPoint(x: width * 0.5, y: height - arcOffset * 2.0))

        // close the path
        bezierPath.close()

        shapeLayer.path = bezierPath.cgPath

    }

}