Swift切指定位置圆角

2022-07-11  本文已影响0人  萤火驻守心间
image.png

使用示例:

        let bgView = UIView()
        bgView.frame = CGRect(x: 0.5*(UIScreen.main.bounds.size.width-100), y: 100, width: 100, height: 100)
        bgView.backgroundColor = .orange
        view.addSubview(bgView)
        bgView.addCorner(conrners: [.topLeft,.topRight], radius: 10)

UIView扩展圆角方法:

extension UIView {
    ///添加部分圆角(自动布局情况下,要在布局后使用)
    func addCorner(conrners: UIRectCorner , radius: CGFloat) {
        self.layoutIfNeeded()
        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
    }
}
上一篇 下一篇

猜你喜欢

热点阅读