【问题标题】:Change .contents of sublayers in a NSMutablearray更改 NSMutablearray 中子层的 .contents
【发布时间】: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


【解决方案1】:

我还没有测试过,但它看起来好像可以工作

[sublayer.contents addObject:[UIImage imageNamed:@"image2.png"]];

【讨论】:

  • [[_layerArray objectAtIndex:(i*HEIGHT)+j ].contents addObject:[UIImage imageNamed:@"image1.png"]];抛出:“在‘id’类型的对象上找不到属性‘内容’”
【解决方案2】:
[[_layerArray objectAtIndex:(i*HEIGHT)+j ] setContents:(id) [UIImage imageNamed:@"image.png"].CGImage];

但是速度很慢……

###最终编辑

作为上述主题发布的方法非常缓慢......我的最终方法如下:

UIViewController 有 UIView 有 CALayers 作为子层

子层是: 1. zindex 0处的背景 2. frontImages 是高 zindexes 的混合图像......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2023-03-13
    • 2014-05-24
    相关资源
    最近更新 更多