CALayer创建透明圆形面具

时间:2014-02-25 15:42:03

标签: 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(CGRectMakeBoundsWithSize(shape.frame.size), NULL);

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

self.layer.mask = shape;

1 个答案:

答案 0 :(得分:11)

是的,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;