如何在 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
的 code>backgroundColor 属性)
然后,如果检测到连接,我会用 2 个 NSPoint
绘制它。这段代码可以工作,但我没有让它在子视图上绘制,只能在第一个 NSView
上绘制。
I'm trying to draw at the top of my NSView
which has some subviews.
In fact I'm trying to reproduce the connection line style of 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
)
Then if a connection if detected, I draw it with 2 NSPoint
s. This code works but I didn't get it to draw over subviews, only on the first NSView
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
父视图无法覆盖其子视图。您必须在子视图上放置另一个视图并在那里画线。
A parent view cannot draw over its subviews. You would have to place another view over the subviews and draw the line there.