Skip to content

Instantly share code, notes, and snippets.

@128keaton
Forked from bgreenlee/flashScreen.swift
Created August 29, 2019 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 128keaton/81da4399f9d005b9b4ae96924f246663 to your computer and use it in GitHub Desktop.
Save 128keaton/81da4399f9d005b9b4ae96924f246663 to your computer and use it in GitHub Desktop.
Flashing the screen in Swift. Sticking this here because it took me a while to work out the right variable types and casting.
func flashScreen() {
let inDuration: CGDisplayFadeInterval = 0.0
let outDuration: CGDisplayFadeInterval = 1.0
let color = NSColor.white
var fadeToken: CGDisplayFadeReservationToken = 0
let colorToUse = color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)!
let err = CGAcquireDisplayFadeReservation(inDuration + outDuration, &fadeToken)
if err != CGError.success {
DDLogError("Error acquiring fade reservation")
return
}
CGDisplayFade(fadeToken, inDuration,
0.0 as CGDisplayBlendFraction, 0.2 as CGDisplayBlendFraction,
Float(colorToUse.redComponent), Float(colorToUse.greenComponent), Float(colorToUse.blueComponent),
boolean_t(1))
CGDisplayFade(fadeToken, outDuration,
0.2 as CGDisplayBlendFraction, 0.0 as CGDisplayBlendFraction,
Float(colorToUse.redComponent), Float(colorToUse.greenComponent), Float(colorToUse.blueComponent),
boolean_t(1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment