图层托管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记载:
当使用一个层托管视图,你不应该依赖于 图纸来看,也不应该添加子视图层托管视图。
什么是真的?示例代码是否有误,它只是巧合而已?或者是文档错误(我怀疑)?还是可以的,因为子视图是通过动画代理添加的?
谢谢非常非常! –