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
[[_layerArray objectAtIndex:(i*HEIGHT)+j ] setContents:(id) [UIImage imageNamed:@"image.png"].CGImage];

しかし、それは非常に遅いです...

### 最終編集

上記のトピックとして投稿されたアプローチは非常に遅くなりました...私の最終的なアプローチは次のとおりです。

UIViewController には UIView があり、サブレイヤーとして CALayers があります

サブレイヤーは次のとおりです。 1. zindex 0 の背景 2. より高い zindexes の種類のブレンドされた画像である frontImages....

于 2013-03-12T21:24:48.653 に答える
0

私はこれをテストしていませんが、うまくいくようです

[sublayer.contents addObject:[UIImage imageNamed:@"image2.png"]];
于 2013-03-11T21:42:04.597 に答える