【发布时间】:2013-03-11 21:15:34
【问题描述】:
我正在创建一个 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 吗?
谢谢!
【问题讨论】:
-
可能是这样的:? id subl = [_layerArray objectAtIndex:(i*HEIGHT)+j ]; [subl.contents addObject:[UIImage imageNamed:@"grass.png"]];
标签: iphone objective-c xcode