Why are 2 lines rendered with this code (drawRect)?

59 views Asked by At

I was wondering why my window looks like this: 2 lines

When I resize the window, one line keeps stretching while the other one keeps statically sized and skewed.

As you can see, one of the lines keeps to the edges.

The strange part is, that I record the height and width of the window whenever drawRect is called.

At the start of the application, the drawRect method gets called 4 times. strange

I looked at this and realized it wasn't the actual size, and moved the window size a bit and got real results. What's strange, is how it jumped so fast.

enter image description here

The strange part is how the "ghost" line is still there, I have no idea which one is the ghost! Why? I changed the color of the line which is displayed... and:

enter image description here

The one which is statically displayed is the one which is "generated" using my code each drawRect.

The code I am using for my drawRect:

- (void)drawRect:(NSRect)dirtyRect
{
    NSLog(@"%f, %f", dirtyRect.size.height, dirtyRect.size.width);
    NSBezierPath *line = [NSBezierPath bezierPath];
    [line moveToPoint:NSMakePoint(NSMaxX([self bounds]), NSMaxY([self   bounds]))];
    //Sorry about formatting; SO doesn't make it easy to format.
    [line lineToPoint:NSMakePoint(NSMinX([self bounds]), NSMinY([self bounds]))];
    [line setLineWidth:_lwith]; /// Make it easy to see
    [_lineColor set]; /// Make future drawing the color of lineColor.

    [line stroke];
}

I am asking for a bit of explanation, or just anything that will help! Thanks

1

There are 1 answers

0
0xA On BEST ANSWER

Self answered

What did I do?: I had my subclass of NSView inside of another NSView in the storyboard; the child NSView was statically kept at a certain size, while the NSView (parent/window) was moving around freely. Keep in mind, I had my instance of the child NSView saved in my ViewController. My NSView was displaying a line no matter what, and it only changed color if I had the instance of itself.

Make sure you have your storyboard all checked, mine looks like this now: enter image description here

Notice how neat it is to have only one "graph" view ;)