我有一個NSView
與自定義的子類,其中繪製一個圓角的矩形網格。這NSView
被放置與接口生成器,並在其上我有一些NSButton
s。自定義NSView繪製在它上面的控件
問題是,有時當視圖重新繪製(即,當我點擊它上面的一個按鈕),然後重新繪製一些按鈕,以保持在頂部。當發生這種情況時,只有較小的圓角矩形出現在按鈕上,而不是在循環之前繪製的背景。
這裏是代碼形式的drawRect:
[NSGraphicsContext saveGraphicsState];
NSBezierPath *path = [NSBezierPath bezierPathWithRect:self.bounds];
[[NSColor grayColor] set];
[path fill];
[NSGraphicsContext restoreGraphicsState];
for(int r = 0; r < 15; r++){
for(int c = 0; c < 15; c++) {
[NSGraphicsContext saveGraphicsState];
// Draw shape
NSRect rect = NSMakeRect(20 * c, 20 * r, 15, 15);
NSBezierPath *roundedRect = [NSBezierPath bezierPathWithRoundedRect: rect xRadius:1 yRadius:1];
[roundedRect setClip];
// Fill
[[NSColor colorWithCalibratedHue:0 saturation:0 brightness:0.3 alpha:1] set];
[roundedRect fill];
// Stroke
[[NSColor colorWithCalibratedHue:0 saturation:0 brightness:0.5 alpha:1] set];
[roundedRect setLineWidth:2.0];
[roundedRect stroke];
[NSGraphicsContext restoreGraphicsState];
}
}
以下是截圖:
更新:簡化代碼,增加了屏幕截圖。
在這種情況下,正確的等級是什麼? –