3

如何在同一个 UIView 中正确使用这两者?

我有一个自定义子类 CALayer,在其中我在 drawInContext 中绘制了一个模式

我有另一个,其中我将覆盖 PNG 图像设置为内容。

我有第三个只是背景。

我如何覆盖所有这 3 个项目?

[self.layer addSublayer:bottomLayer];    // this line is the problem

[squaresLayer drawInContext:viewContext];
[self.layer addSublayer:imgLayer];

如果我按照这个顺序做的话,另外两个自己会正确绘制。无论我尝试将bottomLayer放在哪里,它总是会阻止squaresLayer绘制。我需要 3 层的原因是我打算为背景和自定义层中的颜色设置动画。顶层只是一个图形覆盖。

4

1 回答 1

1

不妨粘贴代码以防万一有人尝试为具有自己的内部绘制例程的堆叠 CALayer 设置动画

- (void)drawRect:(CGRect)rect {

    [imgLayer removeFromSuperlayer];

    CGFloat w = [self.blockViewDelegate w];
    CGFloat h = [self.blockViewDelegate h];

    CGFloat wb = w/4;
    CGFloat hb = h/4;

    CGContextRef viewContext = UIGraphicsGetCurrentContext();

    // Layers

    [bottomLayer sizes:wb :hb :1];
    bottomLayer.frame = CGRectMake(0, 0, w, h);
    bottomLayer.opaque = NO;

    [topLayer sizes:wb :hb :0];
    topLayer.frame = CGRectMake(0, 0, w, h);
    topLayer.opaque = NO;

    // Overlay

    imgLayer.frame = CGRectMake(0, 0, w, h);
    imgLayer.opaque = NO;
    imgLayer.opacity = 1.0f;

    UIImage *overlay = [self.blockViewDelegate image];
    CGImageRef img = overlay.CGImage;
    imgLayer.contents = (id)img;

    // Add layers

    [bottomLayer drawInContext:viewContext];
    [topLayer drawInContext:viewContext];
    [self.layer addSublayer:imgLayer];

}

blockViewDelegate 是我存储宽度、高度和图像信息的地方,它是这个 UIView 的控制器。

topLayer 和 bottomLayer 是自定义 UIView 子类,它们在视图中绘制一些具有可变颜色信息的形状。稍后在我的动画中,我只是用计时器重复运行“setNeedsDisplay”,这个例程重复,层使用更新的参数重新绘制。

于 2009-08-01T22:48:53.967 回答