gpt4 book ai didi

cocoa - 图层托管的 NSView 是否允许有 subview ?

转载 作者:行者123 更新时间:2023-12-03 16:06:55 24 4
gpt4 key购买 nike

层托管的 NSView(因此您为其提供 CALayer 实例并使用 setLayer: 设置它的 NSView)显然可以包含 subview 。为什么很明显?因为在苹果自己的Cocoa Slides sample code project ,您可以选中一个复选框,将 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];
}
}

在同一个 AssetCollectionView 类中,为每个应显示的图像添加 subview :

- (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内容如下:

When using a layer-hosting view you should not rely on the view for drawing, nor should you add subviews to the layer-hosting view.

什么是真的?示例代码是否不正确,它的工作只是巧合?或者文档是假的(我对此表示怀疑)?或者因为 subview 是通过动画代理添加的,所以可以吗?

最佳答案

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

如果将 subview 添加到图层托管 View 中,则它可能不会按照您想要的正确同级顺序出现。另外,我们有时会添加和删除它们,因此它可能会根据您何时调用 setLayer:、setWantsLayer: 或何时在 super View 中添加或删除 View 而改变。在 Lion 上(及之前),当 View 从窗口(或 super View )中删除时,我们会删除我们“拥有”的图层(即:支持的图层)。

添加 subview 是可以的...如果您有不是 NSView 的兄弟层,则子层数组中的 subview 的子级顺序可能不确定。

关于cocoa - 图层托管的 NSView 是否允许有 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10719368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com