0

I am trying to create an application where movies are played in NSViews that may move behind other views. I want to use AVFoundation so I will be rendering video to an AVPlayerLayer.

However, video is always played on top of everything else, and this seems to be an issue with the layers within views.

Consider the following, within a view create:

NSView *backView = [[NSImageView alloc] init];
NSImage *image = [[NSImage alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Background" withExtension:@"png"]];
[backView setImage:image];
[self addSubview:backView];

NSView *layerView = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 100, 500)];
[layerView setWantsLayer:YES];
[layerView.layer setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
[self addSubview:layerView];

NSView *frontView = [[NSImageView alloc] init];
image = [[NSImage alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Foreground" withExtension:@"png"]];
[frontView setImage:image];
[self addSubview:frontView];

Looking at this you will get a white layer rendered on top of everything else. On top of that the white layer won't be affected by the alpha property of the parent view.

Am I making a naive mistake here - I seem to be doing everything as recommended.

Thank you!

Giles
  • 1,428
  • 11
  • 21
  • Chances are that you're getting some strangeness when working with the combination of overlapped layer-backed views and non-layer-backed. Have you tried making the other two layer-backed? – gaige Jun 19 '13 at 23:13
  • Thank you Gaige. That did seem to do the trick. There is still some popping of layers when resizing the window but in general the ordering works as expected. Do you want to add the above as an answer? – Giles Jun 20 '13 at 09:40
  • glad to hear it. I added the answer. Thanks. – gaige Jun 20 '13 at 10:33

1 Answers1

1

Chances are that you're getting some strangeness when working with the combination of overlapped layer-backed views and non-layer-backed. I would try making the other two layer-backed views.

gaige
  • 17,263
  • 6
  • 57
  • 68