在drawRect中使用iOS反转掩码 [英] iOS invert mask in drawRect
本文介绍了在drawRect中使用iOS反转掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码,我成功地屏蔽了我绘图的一部分,但它与我想要屏蔽的相反。这掩盖了绘图的内部,我想掩盖外部。有没有一种简单的方法来反转这个掩码?
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;
这篇关于在drawRect中使用iOS反转掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文