27

使用下面的代码,我成功地掩盖了我的绘图的一部分,但这与我想要掩盖的相反。这掩盖了绘图的内部,我想掩盖外部。有没有一种简单的方法来反转这个面具?

myPath下面是一个UIBezierPath

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef maskPath = CGPathCreateMutable();
CGPathAddPath(maskPath, nil, myPath.CGPath);
[maskLayer setPath:maskPath];
CGPathRelease(maskPath);
self.layer.mask = maskLayer;
4

7 回答 7

41

在形状图层 ( ) 上使用偶数填充,maskLayer.fillRule = kCAFillRuleEvenOdd;您可以添加一个覆盖整个框架的大矩形,然后添加您要屏蔽的形状。这实际上将反转蒙版。

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef maskPath = CGPathCreateMutable();
CGPathAddRect(maskPath, NULL, someBigRectangle); // this line is new
CGPathAddPath(maskPath, nil, myPath.CGPath);
[maskLayer setPath:maskPath];
maskLayer.fillRule = kCAFillRuleEvenOdd;         // this line is new
CGPathRelease(maskPath);
self.layer.mask = maskLayer;
于 2013-09-09T09:59:44.757 回答
19

对于 Swift 3.0

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 = kCAFillRuleEvenOdd
    }

    // Set the mask of the view.
    viewToMask.layer.mask = maskLayer;
}
于 2017-02-14T23:21:40.003 回答
9

根据接受的答案,这是 Swift 中的另一个混搭。我已经把它变成了一个函数,并把它变成了invert可选的

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;
}
于 2015-10-20T18:23:09.207 回答
5

这是我的 Swift 4.2 解决方案,它允许圆角半径

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;
    }

}
于 2019-04-18T08:11:55.293 回答
5

斯威夫特 5

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let red = UIView(frame: view.bounds)
        view.addSubview(red)
        view.backgroundColor = UIColor.cyan
        red.backgroundColor = UIColor.red
        red.mask(CGRect(x: 50, y: 50, width: 50, height: 50), invert: true)
    }
}

extension UIView{

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

666

谢谢@arvidurs

于 2019-09-04T08:33:16.453 回答
3

对于斯威夫特 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;
}
于 2018-12-12T15:56:22.070 回答
0

为了反转掩码,您可以像这样

  1. 在这里,我有交叉矩形的面具,比如

     let crossPath =  UIBezierPath(rect: cutout.insetBy(dx: 30, dy: -5))
              crossPath.append(UIBezierPath(rect: cutout.insetBy(dx: -5, dy: 30)))
                let crossMask = CAShapeLayer()
                crossMask.path = crossPath.cgPath
                crossMask.backgroundColor = UIColor.clear.cgColor
                crossMask.fillRule = .evenOdd
    
  2. 在这里,我在我的交叉矩形周围添加第三个矩形,因此使用 .evenOdd 它占用的面积等于(新矩形 - 旧交叉矩形),换句话说,在交叉矩形的区域之外

      let crossPath = UIBezierPath(rect: cutout.insetBy(dx: -5, dy: -5) )
        crossPath.append(UIBezierPath(rect: cutout.insetBy(dx: 30, dy: -5)))
        crossPath.append(UIBezierPath(rect: cutout.insetBy(dx: -5, dy: 30)))
        let crossMask = CAShapeLayer()
        crossMask.path = crossPath.cgPath
        crossMask.backgroundColor = UIColor.clear.cgColor
        crossMask.fillRule = .evenOdd
    

在此处输入图像描述

于 2020-01-31T22:49:33.790 回答