iOS invert mask in drawRect

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

    With even odd filling on the shape layer (maskLayer.fillRule = kCAFillRuleEvenOdd;) you can add a big rectangle that covers the entire frame and then add the shape you are masking out. This will in effect invert the mask.

    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;
    

提交回复
热议问题