Ios CALayer创建透明圆形遮罩

Ios CALayer创建透明圆形遮罩,ios,iphone,quartz-core,Ios,Iphone,Quartz Core,我想创建一个遮罩层,这是一个圆圈,使圆圈内容透明,并保持周围的一切。不幸的是,下面的代码做的恰恰相反,它画了一个圆,使周围的一切都透明 CAShapeLayer * shape = [CAShapeLayer layer]; shape.frame = CGRectMake((CGSSize().width/2.f)-40.f, -40.f, 80.f, 80.f); CGPathRef pathRef = CGPathCreateWithEllipseInRect(C

我想创建一个遮罩层,这是一个圆圈,使圆圈内容透明,并保持周围的一切。不幸的是,下面的代码做的恰恰相反,它画了一个圆,使周围的一切都透明

CAShapeLayer * shape = [CAShapeLayer layer];
shape.frame          = CGRectMake((CGSSize().width/2.f)-40.f, -40.f, 80.f, 80.f);

CGPathRef pathRef    =
CGPathCreateWithEllipseInRect(CGRectMakeBoundsWithSize(shape.frame.size), NULL);

shape.path            = pathRef;
shape.fillColor       = [UIColor blueColor].CGColor;

self.layer.mask = shape;

是的,kCAFillRuleEvenOdd在没有先添加rect的情况下并没有做到这一点,但这里有一个工作片段:

CAShapeLayer *shape = [CAShapeLayer layer];

shape.frame = self.bounds;

CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathAddRect(pathRef, NULL, self.bounds);
CGPathAddEllipseInRect(pathRef, NULL, self.bounds);

shape.fillRule = kCAFillRuleEvenOdd;
shape.path = pathRef;

self.layer.mask = shape;

可能重复:或者这可能是一个更好的重复:kCAFillRuleEvenOdd不起作用,它保持不变。您还将外部矩形添加到形状中,因此有一些偶数/奇数区域?不,我没有,这就是原因!谢谢你的帮助!是的,谢谢。在层中用作孔的遮罩的关键点是1。可变路径,先添加一个矩形,然后添加椭圆,这样2。kCAFillRuleEvenOdd在椭圆上用作“冲孔”。