iOS 在 drawRect 中反转遮罩 [英] iOS invert mask in drawRect
本文介绍了iOS 在 drawRect 中反转遮罩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码,我成功地掩盖了我的绘图的一部分,但这与我想要掩盖的相反.这掩盖了绘图的内部部分,我想掩盖外部部分.有没有简单的方法可以反转这个掩码?
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 like to mask the outer portion. Is there a simple way to invert this mask?
myPath
下面是一个 UIBezierPath
.
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef maskPath = CGPathCreateMutable();
CGPathAddPath(maskPath, nil, myPath.CGPath);
[maskLayer setPath:maskPath];
CGPathRelease(maskPath);
self.layer.mask = maskLayer;
推荐答案
在形状图层上使用偶数奇数填充 (maskLayer.fillRule = kCAFillRuleEvenOdd;
) 你可以添加一个覆盖整个的大矩形框,然后添加您要遮罩的形状.这实际上将反转掩码.
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;
这篇关于iOS 在 drawRect 中反转遮罩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文