我想创建一个 mask 层,它是一个圆圈,使圆圈内容透明并保持周围的一切。不幸的是,下面的代码恰恰相反,它画了一个圆圈并使周围的一切都变得透明。
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 在不先添加一个矩形的情况下并没有发挥它的魔力,不过这里有一个工作片段:
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;
关于ios - CALayer 创建透明圆形 mask ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22019122/