Drawing lines and points with Cocoa
I'm trying to draw some dots connected with lines. The dot consists of the "nucleus" with orbital area around it.
The problem appears when I try to move those dots which gives me distorted lines:
In my drawRect:
method I iterate over an array of created dots and draw a bezier path by using lineToPoint:
methods.
Dot *prevDot = nil;
NSBezierPath *line = [NSBezierPath bezierPath];
for (Dot *dot in _dots) {
if (!prevDot) {
[line moveToPoint:dot.position];
} else {
[line lineToPoint:dot.position];
}
prevDot = dot;
}
[line stroke];
My question is what technique should I use to imple开发者_JAVA技巧ment clean line update between points once one of them is moved?
Your drawing code is correct. However, you must update a larger portion of the view when it changes. It looks like you're only updating the point which moves, and not the lines. An easy fix is to call:
[myView setNeedsDisplay:YES];
whenever you change the location of the point. This will redraw the entire view. You can use other methods to more selectively update the view only where it changes, which may give you better performance.
Typically, you'd call this in a method invoked from an NSNotification
sent by your data class.
精彩评论