iOS invert mask in drawRect

前端 未结 7 2017
名媛妹妹
名媛妹妹 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

    Based on the accepted answer, here's another mashup in Swift. I've made it into a function and made the invert optional

    class func mask(viewToMask: UIView, maskRect: CGRect, invert: Bool = false) {
        let maskLayer = CAShapeLayer()
        let path = CGPathCreateMutable()
        if (invert) {
            CGPathAddRect(path, nil, viewToMask.bounds)
        }
        CGPathAddRect(path, nil, maskRect)
    
        maskLayer.path = path
        if (invert) {
            maskLayer.fillRule = kCAFillRuleEvenOdd
        }
    
        // Set the mask of the view.
        viewToMask.layer.mask = maskLayer;
    }
    

提交回复
热议问题