开发者

Cocoa drawing glitches when window loses then regains main status

Various things, including (perhaps most notably) the window losing its main status and then being brought back into focus, cause some of my custom views to develop rendering glitches until I do something (such as resize the window) to cause these custom views to redraw. I assume this is due to some sort of caching bug in AppKit, and I'm sure I'm not the only one getting this behaviour, so does anyone know how to work around it?

Here's an example:

Cocoa drawing glitches when window loses then regains main status

Cocoa drawing glitches when window loses then regains main status

Notice the weird dark line at the left of the view's background? This is drawn even without the subviews in the view. I get other similar bugs in some of my other views.

Here's the code that does the drawing:

void EDDrawGlossEffectInRect(NSRect dirtyRect) {
    NSRect topRect, bottomRect;
    NSDivideRect(dirtyRect, &topRect, &bottomRect, (dirtyRect.size.height / 2), NSMaxYEdge);

    [EDLightChromeColor set];
    NSRectFill(topRect);

    [EDMidChromeColor set];
    NSRectFill(bottomRect);
}开发者_如何学C

and

-(void)drawRect:(NSRect)dirtyRect {
    EDDrawGlossEffectInRect(dirtyRect);

    NSBezierPath *path = [NSBezierPath bezierPath];
    [path setLineWidth:1.0];

    NSPoint startPoint = {0, dirtyRect.size.height};
    NSPoint endPoint = {dirtyRect.size.width, dirtyRect.size.height};

    [path moveToPoint:startPoint];
    [path lineToPoint:endPoint];

    [[NSColor colorWithCalibratedWhite:0.7 alpha:1] set];

    [path stroke];
}

The view is created programatically, not with Interface Builder, which I'm not using for this project.


dirtyRect will not necessarily cover your view's visible rect, so you might want to see which part of your view's bounds intersect with the dirtyRect and just repaint that. Otherwise, just repaint the bounds of the view. Resizing the window can cause the entire bounds to be dirty (usually), which is probably why you're seeing full repaints at that point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜