iOS – Calayer creates transparent round mask

I want to create a mask layer, which is a circle, make the circle content transparent, and keep everything around it. Unfortunately, the code below is just the opposite, it draws a circle And make everything 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;
Yes, kCAFillRuleEvenOdd did not do this without first adding rect, here is a working snippet :

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;

I want to create a mask layer, which is a circle, make the circle content transparent, and keep everything around it. Unfortunately, the following The code is just the opposite, it draws a circle and makes everything 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;

Yes, kCAFillRuleEvenOdd did not do this without first adding rect, here is a working snippet:

CAShapeLayer *shape = [CAShapeLayer layer];

shape.frame = self.bounds;

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

shape.fillRule = kCAFillRuleEvenOdd;< br />shape.path = pathRef;

self.layer.mask = shape;

Leave a Comment

Your email address will not be published.