經(jīng)常會(huì)在業(yè)務(wù)中遇到只需要上部圓角的背景惫确,常規(guī)方式有兩種:
1.直接讓設(shè)計(jì)師切圖
2.自己畫
先上代碼
let v = UIView()
v.backgroundColor = .orange
// 圓角大小
let radius: CGFloat = 20;
// 圓角位置
let corner: UIRectCorner = [.topLeft, .topRight]
//frame可以先計(jì)算完成 避免圓角拉伸
let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 310)
let path: UIBezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners: corner, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer: CAShapeLayer = CAShapeLayer()
maskLayer.frame = rect;
maskLayer.path = path.cgPath
v.layer.mask = maskLayer;
return v
上述還有小tips迄损,swift中的option類型的枚舉嗦玖,并不是使用|
運(yùn)算,而是采用集合的形式[.topLeft, .topRight]
效果圖:
that's all~