繁体   English   中英

带有CAShapeLayer遮罩的CAGradientLayer未显示

[英]CAGradientLayer with CAShapeLayer mask not showing

我有以下代码:

- (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];
}

但是它什么也没显示。 不知道我缺少什么(除了我的图形)。

faucetfaucetPath都是属性)

UPDATE

好的,我猜想CAGradientLayer必须具有有意义的frame 我假设CAShapeLayerframe没有那么有意义,因为path可以超出frame之外。 添加以下行使其显示:

self.faucet.frame = CGRectMake(0, 0, 64, 64);

现在,我的问题是,是否有一种方法可以避免这样做? 我只是想将图层的框架隐式地包含在包含视图/图层的框架中。

您需要设置图层的框架。 以下代码将绘制一个红色圆圈:

- (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.

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