美文网首页工作生活
UIView 圆角(Swift)

UIView 圆角(Swift)

作者: GaoEnron | 来源:发表于2019-07-04 10:50 被阅读0次

一、通过添加UIView添加Extension方法添加切指定角圆角操作

1. UIRectCorner 传递相应的枚举类型
public struct UIRectCorner : OptionSet {
    public init(rawValue: UInt)
    public static var topLeft: UIRectCorner { get }
    public static var topRight: UIRectCorner { get }
    public static var bottomLeft: UIRectCorner { get }
    public static var bottomRight: UIRectCorner { get }
    public static var allCorners: UIRectCorner { get }
}
2、根据上面的枚举然后选择想要切的圆角位置

[UIRectCorner.topRight, .topLeft, bottomLeft]

// 切圆角相应的处理操作
extension UIView {
     /// 切圆角处理操作
    ///
    /// - Parameters:
    ///   - width: UIView 宽度
    ///   - height: UIView 高度
    ///   - corners: UIRectCorner 传递枚举数组 [] 例如: [UIRectCorner.topRight, .topLeft]
    ///   - cornerRadii: CGSize 类型
    func clickCorner(width: CGFloat, height: CGFloat, corners: UIRectCorner, cornerRadii: CGSize) {
        let maskBezier = UIBezierPath.init(roundedRect: CGRect.init(x: 0, y: 0, width: width, height: height), byRoundingCorners: corners, cornerRadii: cornerRadii)
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = CGRect.init(x: 0, y: 0, width: width, height: height)
        maskLayer.path = maskBezier.cgPath
        self.layer.mask = maskLayer
    }
}

相关文章

网友评论

    本文标题:UIView 圆角(Swift)

    本文链接:https://www.haomeiwen.com/subject/fqrlhctx.html