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];
CGContextMoveToPo开发者_如何转开发int(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
.
A parent view cannot draw over its subviews. You would have to place another view over the subviews and draw the line there.
精彩评论