是否允许使用图层托管NSView进行子视图?

Joh*_*rug 7 cocoa core-animation calayer nsview

层托管NSView(因此您为其提供CALayer实例并将其设置为的NSView setLayer:)显然可以包含子视图.为什么显然?因为在Apple自己的Cocoa Slides示例代码项目中,您可以选中一个复选框,将复选框切换AssetCollectionView为图层托管:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

在同一个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];
}
Run Code Online (Sandbox Code Playgroud)

当我构建并运行应用程序并使用它时,一切似乎都很好.

但是,在Apple的NSView Class Reference中,setWantsLayer:方法是:

使用图层托管视图时,不应依赖视图进行绘制,也不应将子视图添加到图层托管视图.

什么是真的?示例代码是否不正确,它只是巧合才有效?或者文档是假的(我怀疑)?或者是否可以,因为子视图是通过动画师代理添加的?

cor*_*unn 19

当AppKit是"图层托管"时,我们假设您可能(或可能不)拥有AppKit不知道的整个图层子树.

如果将子视图添加到图层托管视图,则它可能不会以您想要的正确兄弟顺序出现.另外,我们有时会添加和删除它们,因此它可能会根据您调用setLayer:,setWantsLayer:或何时从superview添加或删除视图而更改.在Lion(和之前),我们从窗口(或superview)中删除视图时删除我们"拥有"的图层(即:图层支持).

可以添加子视图......如果您有不是NSView的兄弟层,那么它们在子图层数组中的子兄弟顺序可能不是确定性的.