Swift 设置部分圆角 功能

使用扩展为UIView 添加部分圆角功能:

extension UIView {
    //设置部分圆角
    func setRoundCorners(corners:UIRectCorner,with radii:CGFloat){
        let bezierpath:UIBezierPath = UIBezierPath.init(roundedRect: (self.bounds), byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let shape:CAShapeLayer = CAShapeLayer.init()
        shape.path = bezierpath.cgPath
        
        self.layer.mask = shape
    }
   }

使用方法:

UIView.setRoundCorners(corners: [UIRectCorner.topLeft,UIRectCorner.topRight], with: 10)

你可能感兴趣的:(iOS,swift,半圆角)