我最近将我的OS X App中的一个视图转换为图层托管,并且所有这些视图都在Mountain Lion下运行良好,但是我的一位测试人员抱怨这些图层没有在Snow Leopard下显示。我编写了一个小测试应用程序来执行进一步的测试(源代码here),这个测试应用程序在10.6下也不起作用。
以下是设置图层的主要代码:
- (id)initWithFrame:(NSRect)frameRect
{
NSLog(@"initWithFrame");
self = [super initWithFrame:frameRect];
if (self != nil)
{
srand((unsigned)time(NULL));
_rootLayer = [[CALayer alloc] init];
_rootLayer.delegate = self;
_rootLayer.anchorPoint = CGPointMake(0.0, 0.0);
_rootLayer.frame = NSRectToCGRect([self bounds]);
_rootLayer.needsDisplayOnBoundsChange = NO;
_rootLayer.masksToBounds = YES;
self.layer = _rootLayer;
self.wantsLayer = YES;
_backgroundLayer = [[CALayer alloc] init];
_backgroundLayer.delegate = self;
_backgroundLayer.anchorPoint = CGPointMake(0.5, 0.5);
_backgroundLayer.frame = CGRectInset(NSRectToCGRect([self bounds]), BACKGROUND_INSET, BACKGROUND_INSET);
_backgroundLayer.cornerRadius = 5.0;
_backgroundLayer.needsDisplayOnBoundsChange = NO;
_backgroundLayer.masksToBounds = YES;
[_rootLayer addSublayer:_backgroundLayer];
_mouseLayer = [self _createOtherLayer];
_mouseLayer.opacity = 0.5;
for (unsigned i = 0; i < NUM_OTHER_LAYERS; i++)
_otherLayers[i] = [self _createOtherLayer];
[_backgroundLayer addSublayer:_mouseLayer];
[_rootLayer setNeedsDisplay];
[_backgroundLayer setNeedsDisplay];
[self _positionOtherLayersInRect:frameRect];
_trackingArea = nil;
[self updateTrackingAreas];
}
return self;
}
以下是创建其他图层的方法:
- (CALayer *)_createOtherLayer
{
CALayer *layer = [[CALayer alloc] init];
layer.delegate = self;
layer.anchorPoint = CGPointMake(0.5, 0.5);
layer.bounds = CGRectMake(0.0, 0.0, 64.0, 64.0);
layer.position = CGPointMake(0.0, 0.0);
layer.needsDisplayOnBoundsChange = NO;
layer.masksToBounds = YES;
layer.shadowColor = CGColorGetConstantColor(kCGColorBlack);
layer.shadowOffset = CGSizeMake(2.0, -2.0);
layer.shadowRadius = 2.0;
layer.shadowOpacity = 1.0;
[_backgroundLayer addSublayer:layer];
[layer setNeedsDisplay];
return layer;
}
有人可以建议为什么这些图层在10.6下不起作用吗?
答案 0 :(得分:0)
如果更改会发生什么:
CALayer *layer = [[CALayer alloc] init];
为:
CALayer *layer = [CALayer layer];
不确定为什么它会有所作为,但值得一试。您是否尝试过使用insertSubLayer:atIndex:而不是addSubLayer?