我正在尝试通过 XCode 中的 Objective-C 制作一个简单的 macOS 屏幕保护程序,它只会用白色填充整个屏幕。 (因为this。)很简单,对吧?我也是这么想的,但无论我做什么,我都会得到一个空白的黑屏。看起来我的drawRect方法甚至没有被调用。知道我错过了什么吗?
#import "Blank_WhiteView.h"
@implementation Blank_WhiteView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
[super animateOneFrame];
[self setNeedsDisplay:YES];
return self;
}
- (void)drawRect:(NSRect)rect {
[super drawRect:rect];
CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, NSRectToCGRect(rect));
}
- (BOOL)hasConfigureSheet {
return NO;
}
@end
最佳答案
ScreenSaverView
通过-animateOneFrame
绘制所有内容。为了能够使用 -drawRect
(在您的情况下,您应该,因为您没有对任何内容进行动画处理)实现 animateOneFrame
,以便:
- (void)animateOneFrame {
[self setNeedsDisplay:YES]; // -drawRect will be called next draw loop
}
这样,-drawRect
将在适当的时候被调用。
关于objective-c - 为什么这个非常简单的空白屏幕保护程序不起作用? (是黑色的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12548078/