带有CALayer子类的NSView makeBackingLayer不显示任何输出

时间:2019-01-29 13:44:45

标签: calayer nsview macos-mojave

这是我的问题stop-nsview-drawrect-clearing-before-drawing-lockfocus-no-longer-working-on-mojave的继续。我使用了模式lockFocus-graphics context-flushWindow模式来绘制另一个视图。由于视图的后备存储已更改(对于新的二进制文件),因此在macOS 10.14上不再有效。

我认为,NSView有一个方法makeBackingLayer并非没有。下面的代码片段调用了makeBackingLayer,我可以成功使用CALayer子类“ TransparentLayer”来使用它。到目前为止,在该层上调用setNeedsDisplay之后,确实成功调用了我的子类的drawInContext:。但是我在那里做的任何绘图都不会出现。我究竟做错了什么? (ScreenRect是全屏矩形。)

我的NSView(全屏):

- (id)initWithFrame:(NSRect)frame
{
    ALog(@"");

    self = [super initWithFrame:frame];
    if (self) {
        self.wantsLayer = YES;
    }
    return self;
}

- (BOOL) isOpaque { return NO; }
- (BOOL) wantsLayer { return YES; }  // YES for layers, else drawRect
- (BOOL) wantsUpdateLayer { return YES; }

- (CALayer *)makeBackingLayer
// NB: If self.layer is already set, makeBackingLayer is NOT called
{
    ALog(@"");

    // OWN LAYER TYPE (simply subclass of CALayer)
    TransparentLayer *newLayer = [[TransparentLayer alloc] initWithFrame:ScreenRect];
    return newLayer;
}

- (void)updateLayer { ALog(@"(we do not want this)"); }  // (not called)

- (void)drawInContext:(CGContextRef)ctx { ALog( @"ctx=%p",ctx ); } // (not called)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { ALog( @"layer=%p ctx=%p",layer,ctx ); } // (not called)

- (void)drawRect:(NSRect)dirtyRect { ALog(@""); } // (not called)

TransparentLayer(也是全屏显示):

- (id)initWithFrame:(NSRect)frame
{
    ALog(@"");

    self = [super init];
    if (self) {        
        self.bounds = frame;    // ?
        self.frame = frame;     // ?
        self.contentsRect = frame;

        self.backgroundColor = [NSColor clearColor].CGColor;  // transparent
        self.opaque = NO;            
    }
    return self;
}

- (void)updateLayer
{
    ALog(@"");
}

- (void)_OUT_display
// THIS (display) IS CALLED when this method is present, else drawInContext:
{
    ALog(@"");
}


- (void)drawInContext:(CGContextRef)ctx
// THIS IS CALLED, BUT NO OUTPUT VISIBLE ON SCREEN
// ViewController calls every cycle: [self.stillView.layer setNeedsDisplay];
{
    ALog(@"");

    // Draw a BLUE square 50x50, screen bottom, left
    CGRect R = CGRectMake(0., 0., 50., 50.  );
    CGContextSetRGBFillColor(ctx, 0,0,1, 1);  // BLUE
    CGContextFillRect(ctx, R);
}

0 个答案:

没有答案