swift 自定义圆角
2020-08-04 本文已影响0人
江河_ios
// 直接调用
let maskPath = UIBezierPath.init(roundedRect: self.giveLable.bounds, byRoundingCorners: UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.bottomRight.rawValue | UIRectCorner.topRight.rawValue), cornerRadii: CGSize(width: 40, height:15))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.giveLable.bounds
maskLayer.path = maskPath.cgPath
self.giveLable.layer.mask = maskLayer
//view扩展
extension UIView {
/// 部分圆角
///
/// - Parameters:
/// - corners: 需要实现为圆角的角,可传入多个
/// - radii: 圆角半径
func extCorner(corners: UIRectCorner, radii: CGFloat) {
let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}