iOS invert mask in drawRect

前端 未结 7 2009
名媛妹妹
名媛妹妹 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:17

    For Swift 4.2

    func mask(viewToMask: UIView, maskRect: CGRect, invert: Bool = false) {
        let maskLayer = CAShapeLayer()
        let path = CGMutablePath()
        if (invert) {
            path.addRect(viewToMask.bounds)
        }
        path.addRect(maskRect)
    
        maskLayer.path = path
        if (invert) {
            maskLayer.fillRule = .evenOdd
        }
    
        // Set the mask of the view.
        viewToMask.layer.mask = maskLayer;
    }
    

提交回复
热议问题