0

我正在创建一个 96x 64 的子层网格一次,所以我可以轻松地更新它们的 .contents ......

- (void)createLayer{
    CALayer *currentLayer = self.layer;

    _layerArray = [[NSMutableArray alloc] initWithCapacity:WIDTH*HEIGHT];
    int cellSize = self.bounds.size.width / WIDTH;
    double xOffset = 0;

    CGRect cellFrame = CGRectMake(0, 0, cellSize, cellSize);
    NSUInteger cellIndex = 0;
    cellFrame.origin.x = xOffset;

    for (int i = 0; i < WIDTH; i++)
    {
        cellFrame.origin.y = 0;
        for (int j = 0; j < HEIGHT; j++, cellIndex++)
        {                
            if([[self.levelState.boardChanges objectAtIndex:(i*HEIGHT)+j] intValue]==1){
                {
                    NSNumber *currentCell = [self.levelState.board objectAtIndex:cellIndex];
                    CALayer *sublayer = [CALayer layer];
                    sublayer.frame = cellFrame;
                    if (currentCell.intValue == 1)
                    {
                        sublayer.contents = (id) [UIImage imageNamed:@"image1.png"].CGImage;
                    }
                    else if (currentCell.intValue == 0)
                    {
                        sublayer.contents = (id) [UIImage imageNamed:@"image2.png"].CGImage;
                    }
                    [currentLayer addSublayer:sublayer];
                    [_layerArray addObject:sublayer];
                }
            }
            cellFrame.origin.y += cellSize;
        }
        cellFrame.origin.x += cellSize;
    }
}

但是我确实失败了..我尝试的是类似的东西...

[[_layerArray objectAtIndex:(i*HEIGHT)+j ].contents ] = (id) [UIImage imageNamed:@"image.png"].CGImage;

我如何更改他们的 .contents ?我应该改用 NSArray 吗?

谢谢 !

4

2 回答 2

0

我还没有测试过这个,但它看起来好像它会工作

[sublayer.contents addObject:[UIImage imageNamed:@"image2.png"]];
于 2013-03-11T21:42:04.597 回答
0
[[_layerArray objectAtIndex:(i*HEIGHT)+j ] setContents:(id) [UIImage imageNamed:@"image.png"].CGImage];

但它非常缓慢...

###最终编辑

作为上述主题发布的方法非常缓慢......我的最终方法如下:

UIViewController 有 UIView 有 CALayers 作为子层

子层是: 1. zindex 0 处的背景 2. frontImages,它们是较高 zindex 的混合图像......

于 2013-03-12T21:24:48.653 回答