更改NSMutablearray中子层的.content [英] Change .contents of sublayers in a NSMutablearray
问题描述
我一次创建了一个96x 64的子图层网格,因此我可以轻松更新其.content ...
I am creating a 96x 64 grid of sublayers once,so i can easily update their .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;
}
}
但是我完全失败了..我尝试过的是这样的事情...
However i fail at exactly that.. what i tried is something like that...
[[_layerArray objectAtIndex:(i*HEIGHT)+j ].contents ] = (id) [UIImage imageNamed:@"image.png"].CGImage;
我如何更改其.content?我应该改用NSArray吗?
How do i change their .contents ? Should i use NSArray instead ?
谢谢!
推荐答案
[[_layerArray objectAtIndex:(i*HEIGHT)+j ] setContents:(id) [UIImage imageNamed:@"image.png"].CGImage];
但是它非常慢...
上面作为主题发布的方法要慢很多...我的最终方法是:
the approach posted as topic above was much to slow... my final approach is the following:
UIViewController有UIView将CALayers作为子层
UIViewController has UIView has CALayers as sublayers
子层是:1. zindex 0处的背景2. frontImages是在较高zindex处混合的各种图像....
Sublayers are: 1. The background at zindex 0 2. The frontImages which are blended images of sorts at higher zindexes....
这篇关于更改NSMutablearray中子层的.content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!