Iphone NSMutablearray中子层的内容
我正在创建一个96x64网格的子层,所以我可以很容易地更新它们的内容Iphone NSMutablearray中子层的内容,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我正在创建一个96x64网格的子层,所以我可以很容易地更新它们的内容 - (void)createLayer{ CALayer *currentLayer = self.layer; _layerArray = [[NSMutableArray alloc] initWithCapacity:WIDTH*HEIGHT]; int cellSize = self.bounds.size.width / WIDTH; double xOffset = 0;
- (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;
}
}
如何更改它们的内容?我应该改用NSArray吗
谢谢大家! 我还没有测试过这个,但它看起来似乎可以工作
[sublayer.contents addObject:[UIImage imageNamed:@"image2.png"]];
但是它非常慢
###最终编辑
上面作为主题发布的方法太慢了。。。我的最后方法如下:
UIViewController
他有自己的观点
将Calayer作为子层
子层为:
1.zindex 0处的背景
2.frontImages是以更高的zindex排序的混合图像…[[u layerArray objectAtIndex:(i*HEIGHT)+j].contents addObject:[UIImage imagename:@“image1.png”];抛出一个:“在'id'类型的对象上找不到属性'contents'”可能是这样的:?id subl=[[u layerArray objectAtIndex:(i*高度)+j];[subl.contents addObject:[UIImage ImageName:@“grass.png”];
[sublayer.contents addObject:[UIImage imageNamed:@"image2.png"]];
[[_layerArray objectAtIndex:(i*HEIGHT)+j ] setContents:(id) [UIImage imageNamed:@"image.png"].CGImage];