Created
May 8, 2017 19:59
-
-
Save drosenstark/bb0dbc50d51699dfa0666f6c676306f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Copyright 2017 Dan Rosenstark dr2050.com | |
// See http://stackoverflow.com/a/28570282/8047, too | |
class GameLoop : NSObject { | |
var doSomething: () -> ()! | |
var displayLink : CADisplayLink! | |
init(doSomething: @escaping () -> ()) { | |
self.doSomething = doSomething | |
super.init() | |
start() | |
} | |
// you could overwrite this too | |
func handleTimer() { | |
doSomething() | |
} | |
func start() { | |
displayLink = CADisplayLink(target: self, selector: #selector(handleTimer)) | |
/* | |
* If set to zero, the | |
* display link will fire at the native cadence of the display hardware. | |
* The display link will make a best-effort attempt at issuing callbacks | |
* at the requested rate. | |
*/ | |
displayLink.preferredFramesPerSecond = 0 | |
displayLink.add(to: .main, forMode: .commonModes) | |
} | |
func stop() { | |
displayLink.invalidate() | |
displayLink.remove(from: .main, forMode: .commonModes) | |
displayLink = nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment