swift - view的指定位置切圆角

时间:2024-04-18 12:54:46

1.

extension UIView{
func addCorner(conrners: UIRectCorner , radius: CGFloat) {
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}

2. 使用。 controller 在

    override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//指定位置切圆角
self.bgview.jy.addCorner(conrners: [.topLeft , .topRight], radius: )
}

自定义view 在 draw方法里面写

override func draw(_ rect: CGRect) {
super.draw(rect)
  self.bgview.jy.addCorner(conrners: [.topLeft , .topRight], radius: 6)
}