Basic animation layers not displayed under Snow Leopard

I recently converted one of the views in my OS X application to be layer-level, and everything works well under Mountain Lion, however one of my testers complains that the layers do not display under Snow Leopard. I wrote a small test application to run further tests (source code here ), and this test application also does not work under 10.6.

Here is the main code for the code that sets the layers:

- (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; } 

And here is the method that creates the other layers:

 - (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; } 

Can someone tell me why these layers do not work under 10.6?

+4
source share
2 answers

Have you tried moving code in initWithFrame: to awakeFromNib? This seems to be a fairly common mistake that causes the layers to spin. In this question, the problem was that the layers were set to initWithFrame, but since the feathers were marked as unnecessary by default, they were destroyed right after. Move the code to awakeFromNib, and instead of using the passed frame, use self.frame and see if this fixes the problem. At the very least, it shouldn't be worse (it works on my Mac by running Lion after moving the code to awakeFromNib and it still works fine, so it didn't break anything), and it might just be the solution you are looking for.

Hope this works, or you will soon find another solution. Have a nice day.:)

+2
source

What happens if you change:

 CALayer *layer = [[CALayer alloc] init]; 

in

 CALayer *layer = [CALayer layer]; 

Not sure why this would make a difference, but it might be worth it. Also have you tried using insertSubLayer: atIndex: instead of addSubLayer?

0
source

Source: https://habr.com/ru/post/1434675/


All Articles