答案 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
希望它对你有所帮助