Note that there are some explanatory texts on larger screens.

plurals
  1. PORedrawing NSCollectionView on Scroll Causes Breakup of Graphics
    text
    copied!<p>I have a minor irritant in an <code>NSCollectionView</code> in which the <code>NSCollectionViewItem</code>'s break up visually when I scroll the window.</p> <p>The rate of breakup depends upon the rate of scrolling. For example, if I scroll slowly the breakup occurs more often. Fast scrolling less so. It appears that the problem is when the custom NSView of the <code>NSCollectionViewItem</code> I am using crosses the boundary of the viewable frame.</p> <p>My NSView (custom view of the <code>NSCollectionViewItem</code>) has a very simple drawing algorithm - nothing too complex.</p> <p>Essentially I create a frame within the dirtyRect of the drawRect method and create a few frames inside that:</p> <pre><code>-(void)drawRect:(NSRect)dirtyRect { NSRect mOuterFrame = NSMakeRect(dirtyRect.origin.x, dirtyRect.origin.y, 104, 94); NSRect mSelectedFrame = NSInsetRect(mOuterFrame, 2, 2); NSRect mRedFrame = NSInsetRect(mSelectedFrame, 2, 2); NSRect mInnerFrame = NSInsetRect(mRedFrame, 2, 2); NSBezierPath * mOuterFramePath = [NSBezierPath bezierPathWithRect:mOuterFrame]; NSBezierPath * mSelectedFramePath = [NSBezierPath bezierPathWithRect:mSelectedFrame]; NSBezierPath * mRedFramePath = [NSBezierPath bezierPathWithRect:mRedFrame]; NSBezierPath * mInnerFramePath = [NSBezierPath bezierPathWithRect:mInnerFrame]; [mainBackgroundColor set]; [mOuterFramePath fill]; if (selected) [[NSColor yellowColor] set]; else [mainBackgroundColor set]; [mSelectedFramePath fill]; if (isRedBorder) [[NSColor redColor] set]; else [mainBackgroundColor set]; [mRedFramePath fill]; [[NSColor blackColor] set]; [mInnerFramePath fill]; } </code></pre> <p>I have tried locking the focus and releasing before and after the code as well as setting the graphics context and restoring it - none of which seem to solve the problem.</p> <p>I am using Snow Leopard - not that I think that makes a difference.</p> <p><strong>Solution Update</strong></p> <p>For anyone interested here is the solution to the problem as recommended by NSResponder. I was creating the initial <code>mOuterFrame</code> based upon the <code>drawRect:</code> methods <code>dirtyRect</code> which, as pointed out, was the incorrect thing to do. A quick change from:</p> <pre><code>NSRect mOuterFrame = NSMakeRect(dirtyRect.origin.x, dirtyRect.origin.y, 104, 94); </code></pre> <p>To a 0 based origination point:</p> <pre><code>NSRect mOuterFrame = NSMakeRect(0, 0, 104, 94); </code></pre> <p>I also tweaked the efficiency of the code, being as I am using only rectangles, as suggested although the code change above was enough to resolve the problem in itself. I had to add a change to get a two pixel line. New method:</p> <pre><code>-(void)drawRect:(NSRect)dirtyRect { NSRect mOuterFrame = NSMakeRect(0, 0, 104, 94); NSRect mSelectedFrame = NSInsetRect(mOuterFrame, 2, 2); NSRect mRedFrame = NSInsetRect(mSelectedFrame, 2, 2); NSRect mInnerFrame = NSInsetRect(mRedFrame, 2, 2); [NSBezierPath setDefaultLineWidth:2.0]; [mainBackgroundColor set]; [NSBezierPath strokeRect:mOuterFrame]; if (selected) [[NSColor yellowColor] set]; else [mainBackgroundColor set]; [NSBezierPath strokeRect:mSelectedFrame]; if (isRedBorder) [[NSColor redColor] set]; else [mainBackgroundColor set]; [NSBezierPath strokeRect:mRedFrame]; [[NSColor blackColor] set]; [NSBezierPath strokeRect:mInnerFrame]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload