[英]How to draw over a subview of NSView
I'm trying to draw at the top of my NSView
which has some subviews. 我正在尝试绘制我的
NSView
顶部,它有一些子视图。 In fact I'm trying to reproduce the connection line style of Interface Builder. 实际上我正在尝试重现Interface Builder的连接线样式。 Here is the code I'm using for the moment:
这是我目前使用的代码:
- (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);
}
}
The first part is to color my NSView
(if you know an other way, tell me please 'cause I come from iPhone development and I miss the backgroundColor
property of UIView
) 第一部分是为我的
NSView
着色(如果你知道另一种方式,请告诉我'因为我来自iPhone开发而我错过了UIView
的backgroundColor
属性)
Then if a connection if detected, I draw it with 2 NSPoint
s. 然后,如果检测到连接,我用2
NSPoint
s绘制它。 This code works but I didn't get it to draw over subviews, only on the first NSView
. 这段代码可以工作,但我没有得到它来绘制子视图,只在第一个
NSView
。
A parent view cannot draw over its subviews. 父视图无法绘制其子视图。 You would have to place another view over the subviews and draw the line there.
您必须在子视图上放置另一个视图并在那里绘制线条。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.