How to make a macOS screen saver
First, make sure you really want to. macOS’s screen saver system is absurdly buggy and broken. It’s frustrating to work with and very difficult to make work right.
If you’re determined, read on.
Screen savers are basically just applications. Same bundle format and structure, just with a “saver” extension instead of “app”.
Setting up the Xcode project
In Xcode, create a new project using the Screen Saver template.
Delete the Objective-C code & header files it creates by default (unless, I suppose, you want to write your screen saver in Objective-C – woo, retro! 😆).
You need to import the ScreenSaver module (framework), subclass ScreenSaverView, and implement a couple of method overrides. Here’s the basic skeleton:
import ScreenSaver
class MyScreenSaver: ScreenSaverView {
override init?(frame: NSRect, isPreview: Bool) {
super.init(frame: frame, isPreview: isPreview)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
private func setup() {
// TODO
}
override func startAnimation() {
super.startAnimation()
// TODO
}
override func animateOneFrame() { // Optional.
// TODO
}
override func stopAnimation() { // Only for the live preview in System Settings.
// TODO
super.stopAnimation()
}
override var hasConfigureSheet: Bool {
true
}
private var configureSheetController: ConfigureSheetController?
override var configureSheet: NSWindow? {
configureSheetController = ConfigureSheetController(windowNibName: "ConfigureSheet")
return configureSheetController?.window
}
}
class ConfigureSheetController: NSWindowController {
override var windowNibName: NSNib.Name? {
return "ConfigureSheet"
}
override func windowDidLoad() {
super.windowDidLoad()
// TODO
}
@IBAction func okButtonClicked(_ sender: NSButton) {
// TODO…评论
?
参与讨论