Skip to content

Instantly share code, notes, and snippets.

@bgreenlee
Created April 1, 2015 23:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bgreenlee/4224f678862876364f90 to your computer and use it in GitHub Desktop.
Save bgreenlee/4224f678862876364f90 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.5
let outDuration: CGDisplayFadeInterval = 0.5
let color = NSColor.redColor()
var fadeToken: CGDisplayFadeReservationToken = 0
let colorToUse = color.colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
let err = CGAcquireDisplayFadeReservation(inDuration + outDuration, &fadeToken)
if Int(err) != Int(kCGErrorSuccess.value) {
NSLog("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, inDuration,
0.2 as CGDisplayBlendFraction, 0.0 as CGDisplayBlendFraction,
Float(colorToUse.redComponent), Float(colorToUse.greenComponent), Float(colorToUse.blueComponent),
boolean_t(1))
}
@JackBCousineau
Copy link

Thank you so much, this is literally the only answer on the internet.

@thatcherclough
Copy link

I am getting Use of undeclared type errors. how would i fix this. Sorry I am new to swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment