简体   繁体   English

CALayer创建透明圆形面具

[英]CALayer create transparent round mask

I want to create a mask layer which is a circle that makes the circle content transparent and keep everything around. 我想创建一个遮罩层,这是一个圆圈,使圆圈内容透明,并保持周围的一切。 Unfortunately, the following code does the opposite, it draws a circle and make everything around transparent. 不幸的是,下面的代码恰恰相反,它绘制了一个圆圈并使一切变得透明。

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;

Yeah, kCAFillRuleEvenOdd didn't do it's magic without adding a rect first, here is a working snippet though: 是的,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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM