美文网首页swift4.0
ios UIView任意位置裁剪圆角

ios UIView任意位置裁剪圆角

作者: 279cb620c509 | 来源:发表于2018-11-21 10:59 被阅读51次

    UIView任意位置裁剪圆角

    extension UIView {
    
    /// 裁剪 view 的圆角
    
        func clipRectCorner(direction: UIRectCorner, cornerRadius: CGFloat) {
    
            let cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
    
            let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: direction, cornerRadii: cornerSize)
    
            let maskLayer = CAShapeLayer()
    
            maskLayer.frame = bounds
    
            maskLayer.path = maskPath.cgPath
    
            layer.addSublayer(maskLayer)
    
            layer.mask = maskLayer
    
        }
    
    
    
    }
    

    例:

    view.clipRectCorner(direction: [.topLeft,.topRight], cornerRadius: 10.0)
    

    相关文章

      网友评论

        本文标题:ios UIView任意位置裁剪圆角

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