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

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

我有另一个我将叠加PNG图像设置为内容。

我有第三个只是背景。

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

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

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

如果按顺序执行,其他2本身就会正确绘制。无论我尝试放置bottomLayer,它总是阻止squareLayer绘制。我需要3层的原因是我打算为背景和自定义图层中的颜色设置动画。顶层只是一个图形叠加。

有帮助吗?

解决方案

如果有人试图为具有自己内部绘制例程的堆叠CALayers设置动画,也可以粘贴代码

- (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”。重复使用计时器并重复此程序,使用更新的参数重新绘制图层。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top