是否可以创建具有弯曲底边的UIView。

时间:2017-11-24 12:26:23

标签: ios objective-c uiview

我需要创建具有弯曲底边的UIView,如下所示。最简单的方法是什么?

enter image description here

2 个答案:

答案 0 :(得分:2)

只需将IB中的视图设置为ShapedView类,或者从代码创建它并在绘制路径时使用控制点播放。

class ShapedView: UIView {
    let subLayer: CAShapeLayer = {
        let subLayer = CAShapeLayer()
        subLayer.fillColor = UIColor.red.cgColor
        return subLayer
    }()

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

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

    private func commonInit() {
        layer.addSublayer(subLayer)
        subLayer.frame = bounds
        setPath()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        subLayer.frame = bounds
        setPath()
    }

    private func setPath() {
        let path = UIBezierPath()
        path.move(to: CGPoint(x:0, y:bounds.height))
        path.addQuadCurve(to: CGPoint(x:bounds.width, y:bounds.height), controlPoint: CGPoint(x:bounds.width/2, y:4*bounds.height/5))
        path.addLine(to: CGPoint(x:bounds.width, y:0))
        path.addLine(to: CGPoint(x:0, y:0))
        subLayer.path = path.cgPath
    }
}

答案 1 :(得分:1)

您可以使用CAShapeLayer

实现这一目标

请执行以下操作

1)创建CAShapeLayer

2)创建UIBezierPath

3)将path分配给CAShapeLayer

4)将CAShapeLayer添加到mask

UIVIew

希望它对你有所帮助