Macos CGLayers未绘制到屏幕

Macos CGLayers未绘制到屏幕,macos,cocoa,drawing,quartz-2d,Macos,Cocoa,Drawing,Quartz 2d,我的drawRect:代码有什么问题 为图形创建CGContextRef CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort]; 使用当前上下文创建CGLayerRef CGLayerRef theLayer = CGLayerCreateWithContext(myContext, CGSizeMake(5, 5) NULL); 为层本身创建CGContextRef CGCon

我的drawRect:代码有什么问题

为图形创建CGContextRef

   CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
使用当前上下文创建CGLayerRef

   CGLayerRef theLayer = CGLayerCreateWithContext(myContext, CGSizeMake(5, 5) NULL);
为层本身创建CGContextRef

   CGContextRef theCurrentLayer = CGLayerGetContext(theLayer);
获取要用作原点的鼠标位置

   NSPoint point = [self.window mouseLocationOutsideOfEventStream];
将颜色设置为黑色

   CGContextSetRGBFillColor(theCurrentLayer, 0, 0, 0, 1);
在图层上绘制一个矩形

   CGContextFillRect (theCurrentLayer, CGRectMake (point.x, point.y, 5, 5));
在鼠标位置将图层绘制到当前上下文

   CGContextDrawLayerAtPoint(myContext, CGPointMake(point.x, point.y), theLayer);

什么也没有发生……内存使用量稳步增加,表明正在创建图层,但窗口中没有显示任何内容。

如果您不使用鼠标,该功能是否有效?如果是,您可能必须计算视图坐标系中的鼠标位置。您是否已将此自定义视图作为子视图添加到视图控制器中?