NSViews lớp lưu trữ (do đó NSViews mà bạn cung cấp một cá thể CALayer và đặt nó với setLayer:
) rõ ràng có thể chứa các bản xem trước. Tại sao rõ ràng? Bởi vì trong Apple của riêng Cocoa Slides sample code project, bạn có thể kiểm tra một hộp kiểm mà chuyển mạch AssetCollectionView
khỏi bị lớp hậu thuẫn để trở thành lớp lưu trữ:NSViews lưu trữ lớp có được phép xem phụ không?
- (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];
}
}
Trong cùng lớp AssetCollectionView
, subviews được thêm vào cho mỗi hình ảnh đó sẽ được hiển thị:
- (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];
}
Khi tôi tạo và chạy ứng dụng và chơi cùng với nó, mọi thứ có vẻ ổn.
Tuy nhiên, trong Apple's NSView Class Reference for the setWantsLayer:
method nó đọc:
Khi sử dụng một cái nhìn lớp lưu trữ bạn không nên dựa vào quan điểm cho vẽ, cũng không phải bạn nên thêm subviews đến xem lớp-hosting.
Điều gì là đúng? Mã mẫu có đúng không và nó chỉ là trùng hợp ngẫu nhiên khi nó hoạt động? Hoặc là tài liệu sai (mà tôi nghi ngờ)? Hoặc là nó OK vì các subviews được thêm thông qua proxy animator?
Cảm ơn bạn rất rất nhiều! –