iOS invert mask in drawRect

前端 未结 7 2005
名媛妹妹
名媛妹妹 2020-12-08 07:54

With the code below, I am successfully masking part of my drawing, but it\'s the inverse of what I want masked. This masks the inner portion of the drawing, where I would li

相关标签:
7条回答
  • 2020-12-08 08:34

    Here's my Swift 4.2 solution that allows a corner radius

    extension UIView {
    
        func mask(withRect maskRect: CGRect, cornerRadius: CGFloat, inverse: Bool = false) {
            let maskLayer = CAShapeLayer()
            let path = CGMutablePath()
            if (inverse) {
                path.addPath(UIBezierPath(roundedRect: self.bounds, cornerRadius: cornerRadius).cgPath)
            }
            path.addPath(UIBezierPath(roundedRect: maskRect, cornerRadius: cornerRadius).cgPath)
    
            maskLayer.path = path
            if (inverse) {
                maskLayer.fillRule = CAShapeLayerFillRule.evenOdd
            }
    
            self.layer.mask = maskLayer;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题