Skip to content

Instantly share code, notes, and snippets.

@eyeplum
Last active October 30, 2023 04:17
Show Gist options
  • Save eyeplum/353be4bd4e62fa05cbf1 to your computer and use it in GitHub Desktop.
Save eyeplum/353be4bd4e62fa05cbf1 to your computer and use it in GitHub Desktop.
CVDisplayLinkOutputCallback in Swift
private func createDisplayLink() {
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink)
guard let displayLink = displayLink else {
return
}
let callback: CVDisplayLinkOutputCallback = { (_, _, _, _, _, userInfo) -> CVReturn in
let myView = Unmanaged<MyView>.fromOpaque(COpaquePointer(userInfo)).takeUnretainedValue()
dispatch_async(dispatch_get_main_queue()) {
myView.update()
}
return kCVReturnSuccess
}
let userInfo = UnsafeMutablePointer<Void>(Unmanaged.passUnretained(self).toOpaque())
CVDisplayLinkSetOutputCallback(displayLink, callback, userInfo)
CVDisplayLinkStart(displayLink)
}
private func update() {
// Do your view updates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment