[英]CAGradientLayer with CAShapeLayer mask not showing
I have the following code: 我有以下代码:
- (void)setupLayer {
self.faucetShape = [CAShapeLayer layer];
self.faucetShape.strokeColor = [[UIColor blackColor] CGColor];
self.faucetShape.lineWidth = 2;
self.faucetShape.fillColor = nil;
self.faucetShape.path = [self faucetPath].CGPath; // faucetPath returns a 4 point diamond shaped UIBezierPath*
self.faucet = [CAGradientLayer layer];
self.faucet.mask = self.faucetShape;
self.faucet.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor redColor].CGColor];
[self.layer addSublayer: self.faucet];
}
But it shows nothing. 但是它什么也没显示。 Not sure what I'm missing (other than my graphic). 不知道我缺少什么(除了我的图形)。
( faucet
and faucetPath
are both properties) ( faucet
和faucetPath
都是属性)
UPDATE UPDATE
OK, I guess a CAGradientLayer
has to have a meaningful frame
. 好的,我猜想CAGradientLayer
必须具有有意义的frame
。 I assume a CAShapeLayer
's frame
isn't as meaningful, since the path
can go outside the frame
. 我假设CAShapeLayer
的frame
没有那么有意义,因为path
可以超出frame
之外。 Adding the following line made it show up: 添加以下行使其显示:
self.faucet.frame = CGRectMake(0, 0, 64, 64);
So NOW my question is, is there a way to avoid having to do that? 现在,我的问题是,是否有一种方法可以避免这样做? I'd just like the frame of the layer to implicitly be that of the containing View/Layer. 我只是想将图层的框架隐式地包含在包含视图/图层的框架中。
You need to set the frame of the layers. 您需要设置图层的框架。 The following code will draw a red circle: 以下代码将绘制一个红色圆圈:
- (void)setupLayer
{
CAShapeLayer *faucetShape = [CAShapeLayer layer];
faucetShape.frame = CGRectMake(0, 0, 100, 100);
faucetShape.strokeColor = [[UIColor blackColor] CGColor];
faucetShape.lineWidth = 2;
faucetShape.fillColor = nil;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, nil, CGRectMake(0, 0, 100, 100));
faucetShape.path = path;
CGPathRelease(path);
CAGradientLayer *faucet = [CAGradientLayer layer];
faucet.frame = CGRectMake(10, 10, 100, 100);
faucet.mask = faucetShape;
faucet.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor redColor].CGColor];
[self.view.layer addSublayer: faucet];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.