[英]How to draw over a subview of NSView
我正在尝试绘制我的NSView
顶部,它有一些子视图。 实际上我正在尝试重现Interface Builder的连接线样式。 这是我目前使用的代码:
- (void)drawRect:(CGRect)dirtyRect
{
// Background color
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
// Draw line
if(_connecting)
{
CGContextRef c = [[NSGraphicsContext currentContext] graphicsPort];
[[NSColor redColor] setStroke];
CGContextMoveToPoint(c, _start.x, _start.y);
CGContextAddLineToPoint(c, _end.x, _end.y);
CGContextSetLineWidth(c, LINE_WIDTH);
CGContextClosePath(c);
CGContextStrokePath(c);
}
}
第一部分是为我的NSView
着色(如果你知道另一种方式,请告诉我'因为我来自iPhone开发而我错过了UIView
的backgroundColor
属性)
然后,如果检测到连接,我用2 NSPoint
s绘制它。 这段代码可以工作,但我没有得到它来绘制子视图,只在第一个NSView
。
父视图无法绘制其子视图。 您必须在子视图上放置另一个视图并在那里绘制线条。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.