簡體   English   中英

CALayer創建透明圓形面具

[英]CALayer create transparent round 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在沒有首先添加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