我创建一个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吗?
谢谢!
答案 0 :(得分:0)
我没有对此进行测试,但看起来好像可以正常使用
[sublayer.contents addObject:[UIImage imageNamed:@"image2.png"]];
答案 1 :(得分:0)
[[_layerArray objectAtIndex:(i*HEIGHT)+j ] setContents:(id) [UIImage imageNamed:@"image.png"].CGImage];
但它很慢......
###最终编辑上面发布的主题方法很慢......我的最终方法如下:
的UIViewController 有UIView 将CALayers作为子图层
子层是: 1. zindex 0的背景 2. frontImages是在更高的zindexes上混合的图像......