2009-02-28 36 views
0

我可能只是錯過了一些非常簡單的東西,但每當我在視圖可見時添加CALayers時,它們都不會渲染。下面是我使用的代碼:CALayer在視圖可見時未添加渲染

[[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];

我也嘗試重用相同的層和只設置的內容,以不同的CGImage(通過setContents :),這也使得該層停止渲染(更具體它動畫好像不透明度已被設置爲0,但仍認爲它是1)。我已經通過枚舉視圖圖層的子圖層來驗證圖層實際上在視圖的圖層樹中。他們似乎有正確的形象,並處於正確的位置,他們只是不畫。任何幫助將不勝感激。

回答

1

事實證明,這是由於錯字造成的。在我打電話給setContents的行中,我將它發送給UIImage而不是CGImage。發送正確的類型會導致預期的行爲。