圖層託管NSViews(因此您提供CALayer實例並將其設置爲setLayer:
的NSViews)顯然可以包含子視圖。爲什麼顯然?因爲在蘋果自己的Cocoa Slides sample code project,您可以檢查被層爲後盾,以被層託管切換AssetCollectionView
複選框:圖層託管NSView允許有子視圖嗎?
- (void)setUsesQuartzCompositionBackground:(BOOL)flag {
if (usesQuartzCompositionBackground != flag) {
usesQuartzCompositionBackground = flag;
/* We can display a Quartz Composition in a layer-backed view tree by
substituting our own QCCompositionLayer in place of the default automanaged
layer that AppKit would otherwise create for the view. Eventually, hosting of
QCViews in a layer-backed view subtree may be made more automatic, rendering
this unnecessary. To minimize visual glitches during the transition,
temporarily suspend window updates during the switch, and toggle layer-backed
view rendering temporarily off and back on again while we prepare and set the
layer.
*/
[[self window] disableScreenUpdatesUntilFlush];
[self setWantsLayer:NO];
if (usesQuartzCompositionBackground) {
QCCompositionLayer *qcLayer = [QCCompositionLayer compositionLayerWithFile:[[NSBundle mainBundle] pathForResource:@"Cells" ofType:@"qtz"]];
[self setLayer:qcLayer];
} else {
[self setLayer:nil]; // Discard the QCCompositionLayer we were using, and let AppKit automatically create self's backing layer instead.
}
[self setWantsLayer:YES];
}
}
同樣AssetCollectionView
類,子視圖增加了應顯示的每個圖像:
- (AssetCollectionViewNode *)insertNodeForAssetAtIndex:(NSUInteger)index {
Asset *asset = [[[self assetCollection] assets] objectAtIndex:index];
AssetCollectionViewNode *node = [[AssetCollectionViewNode alloc] init];
[node setAsset:asset];
[[self animator] addSubview:[node rootView]];
[nodes addObject:node];
return [node autorelease];
}
當我構建並運行應用程序並使用它時,一切似乎都沒有問題。
然而,在Apple's NSView Class Reference for the setWantsLayer:
method記載:
當使用一個層託管視圖,你不應該依賴於 圖紙來看,也不應該添加子視圖層託管視圖。
什麼是真的?示例代碼是否有誤,它只是巧合而已?或者是文檔錯誤(我懷疑)?還是可以的,因爲子視圖是通過動畫代理添加的?
謝謝非常非常! –