【问题标题】:CAGradientLayer with CAShapeLayer mask not showing带有 CAShapeLayer 蒙版的 CAGradientLayer 未显示
【发布时间】:2014-01-14 18:56:15
【问题描述】:

我有以下代码:

- (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 都是属性)

更新

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

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

所以现在我的问题是,有没有办法避免这样做?我只是希望图层的框架隐含地成为包含视图/图层的框架。

【问题讨论】:

  • 它们不是弱属性,不是吗?
  • 轻笑,不 :) 他们是(strong, nonatomic)
  • 您是否检查过您的faucetPath 方法是否正在返回您想要的结果?
  • 不@Unheilig,这不起作用。父级的框架是{{256, 0}, {64, 64}}。也许如果我使用边界。

标签: ios calayer


【解决方案1】:

您需要设置图层的框架。下面的代码会画一个红圈:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2018-08-06
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多