CALayer not rendering when added after view visible

ⅰ亾dé卋堺 提交于 2020-01-06 05:00:06

问题


I'm probably just missing something really simple but whenever I add CALayers after the view is visible they don't render. Here's the code I'm using:

[[imageLayers objectAtIndex:0] removeFromSuperlayer];
[imageLayers removeObjectAtIndex:0];
[[imageLayers objectAtIndex:0] removeFromSuperlayer];
[imageLayers removeObjectAtIndex:0];
firstImageOffset = (firstImageOffset + 2) % [pieceCache count];
int topIndex = (firstImageOffset + 6) % [pieceCache count];
int bottomIndex = (firstImageOffset + 7) % [pieceCache count];
float xPos = [(CALayer*)[imageLayers lastObject] position].x + kShiftXPixels;

CALayer* layer1 = [CALayer layer];
[layer1 setFrame:CGRectMake(0, 0, kImageWidth, kImageHeight)];
[layer1 setContents:(id)[UIImage imageNamed:[[pieceCache objectAtIndex:topIndex] objectForKey:@"image_name"]]];
[[[self view] layer] addSublayer:layer1];
[layer1 setPosition:CGPointMake(xPos, kTopRowYPos)];
[imageLayers addObject:layer1];

CALayer* layer2 = [CALayer layer];
[layer2 setFrame:CGRectMake(0, 0, kImageWidth, kImageHeight)];
[layer2 setContents:(id)[UIImage imageNamed:[[pieceCache objectAtIndex:bottomIndex] objectForKey:@"image_name"]]];
[[[self view] layer] addSublayer:layer2];
[layer2 setPosition:CGPointMake(xPos, kBottomRowYPos)];
[imageLayers addObject:layer2];

I've also tried reusing the same layers and just setting the contents to a different CGImage (via setContents:), this also causes the layer to stop rendering (more specifically it animates as if the opacity has been set to 0 thought it's still 1). I've verified that the layers are actually in the view's layer tree by enumerating the view layer's sublayers. They appear to have the right images and be in the correct positions, they just don't draw. Any help would be greatly appreciated.


回答1:


It turns out this resulted from a typo. In the lines where I'm calling setContents, I'm sending it a UIImage rather than a CGImage. Sending the correct type resulted in the expected behavior.



来源:https://stackoverflow.com/questions/598659/calayer-not-rendering-when-added-after-view-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!