我正在创建一个 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 吗?
谢谢 !