微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

清除UIBezierPath上的背景色并填充

如何解决清除UIBezierPath上的背景色并填充

我想查看底部有一条曲线。

我创建的ArchView可以完美地完成工作,除了一件事情,蓝色的部分我希望它是透明的。

enter image description here

如果我将背景色设置为.clear,则会发生以下情况:

enter image description here

这是ArchView代码

class ArchedView: UIView {
    let archHeight: CGFloat
    let bColor: UIColor
    let archColor: UIColor
    let contentView: UIView = UIView()

    init(archHeight: CGFloat,bColor: UIColor,archColor: UIColor) {
        self.archHeight = archHeight
        self.bColor = bColor
        self.archColor = archColor
        super.init(frame: .zero)
        backgroundColor = .clear
        addSubview(contentView)

        contentView.backgroundColor = bColor
        contentView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-archHeight)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        let height: CGFloat = archHeight
        let ovalRect = CGRect(x: 0,y: 0,width: frame.width,height: height)
        let ovalPath = UIBezierPath()
        ovalPath.addArc(withCenter: CGPoint(x: 0,y: 0),radius: (ovalRect.width / 2) * 1.3,startAngle: 180 * .pi/180,endAngle: 0 * .pi/180,clockwise: false)
        ovalPath.addLine(to: CGPoint.zero)

        ovalPath.close()

        var ovalTransform = CGAffineTransform(translationX: ovalRect.midX,y: frame.height - height)
        ovalTransform = ovalTransform.scaledBy(x: 1,y: height / ovalPath.bounds.height)
        ovalPath.apply(ovalTransform)

        archColor.setFill()
        ovalPath.fill()
    }
}

我假设它与fill()有关,但是我不明白这里发生了什么。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。