【发布时间】:2015-05-20 11:31:00
【问题描述】:
我正在尝试创建一个自定义 NSView,它承载 CALayer 层次结构以执行高效显示。然后这个NSView 被嵌入到一个NSTableCellView 中,该NSTableCellView 由基于视图的NSOutlineView 显示。
问题是每当我展开或折叠一个项目时,所有行都会被移动,但图层的内容仍然显示在更改轮廓之前的位置。
滚动NSOutlineView 似乎会刷新图层,并且此时它们会与它们的行重新同步。
我已经使用 Instruments 调试了这种行为,似乎滚动会引发布局操作,该操作会使用展开或折叠项目时应该发生的 setPosition: 调用更新图层。
这里是托管NSView 子类的简单层的一些示例代码。
@interface TestView : NSView
@end
@implementation TestView
- (instancetype)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
CAShapeLayer* layer = [CAShapeLayer layer];
layer.bounds = self.bounds;
layer.position = CGPointMake(NSMidX(self.bounds), NSMidY(self.bounds));
layer.path = [NSBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
layer.fillColor = [NSColor redColor].CGColor;
layer.delegate = self;
self.layer = layer;
self.wantsLayer = YES;
return self;
}
@end
我已经尝试了很多潜在的解决方案来解决这个问题,但我找不到任何有趣的方法可以在 NSView 实例上调用,可以重写以调用 [self.layer setNeedsDisplay] 或 [self.layer setNeedsLayout]。我还在CALayer 本身上尝试了各种设置器,例如:
layer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
layer.needsDisplayOnBoundsChange = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
谁能帮我弄清楚如何让这个图层在NSOutlineView 中正确显示?
【问题讨论】:
标签: objective-c macos cocoa calayer nsoutlineview