Objective c 为什么我会得到一个带有“逆”的面具;CGMutablePathRef“;方法

Objective c 为什么我会得到一个带有“逆”的面具;CGMutablePathRef“;方法,objective-c,ios,path,core-graphics,layer,Objective C,Ios,Path,Core Graphics,Layer,我得出一个观点。视图s帧=(0,0,44,44),视图s背景色为黑色。 我想添加一个面具,使视图看起来像:面具是一个小圆圈在中间的观点。但我得到了相反的结果,只有视图的中间部分没有被遮掩。这样的错误结果 我的代码是: aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [aView.layer setBackgroundColor:[[UIColor blackColor] CGColor]]; CAShapeLa

我得出一个观点。视图
s帧=(0,0,44,44),视图
s背景色为黑色。
我想添加一个面具,使视图看起来像:面具是一个小圆圈在中间的观点。但我得到了相反的结果,只有视图的中间部分没有被遮掩。这样的错误结果

我的代码是:

aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];

[aView.layer setBackgroundColor:[[UIColor blackColor] CGColor]];

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.frame = aView.bounds;
CGMutablePathRef p2 = CGPathCreateMutable();
CGPathAddEllipseInRect(p2, NULL, CGRectInset(mask.bounds, 10, 10));

mask.path = p2;
aView.layer.mask = mask;

CGPathRelease(p2);

[self addSubview:aView];

代码有什么问题?谢谢。

首先,在屏蔽路径中添加一个矩形

其次,将形状层的
fillRule
属性设置为
kCAFillRuleEvenOdd
,使中心部分保持中空状态

aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];

[aView.layer setBackgroundColor:[[UIColor blackColor] CGColor]];

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.fillRule = kCAFillRuleEvenOdd;
mask.frame = aView.bounds;
CGMutablePathRef p2 = CGPathCreateMutable();
CGPathAddRect(p2, &CGAffineTransformIdentity, mask.bounds);
CGPathCloseSubpath(p2);
CGPathAddEllipseInRect(p2, NULL, CGRectInset(mask.bounds, 10, 10));

mask.path = p2;
aView.layer.mask = mask;

CGPathRelease(p2);

首先,将矩形添加到遮罩路径

其次,将形状层的
fillRule
属性设置为
kCAFillRuleEvenOdd
,使中心部分保持中空状态

aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];

[aView.layer setBackgroundColor:[[UIColor blackColor] CGColor]];

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.fillRule = kCAFillRuleEvenOdd;
mask.frame = aView.bounds;
CGMutablePathRef p2 = CGPathCreateMutable();
CGPathAddRect(p2, &CGAffineTransformIdentity, mask.bounds);
CGPathCloseSubpath(p2);
CGPathAddEllipseInRect(p2, NULL, CGRectInset(mask.bounds, 10, 10));

mask.path = p2;
aView.layer.mask = mask;

CGPathRelease(p2);