Created
February 17, 2015 20:20
-
-
Save drosenstark/dbdf0f38e2ae22761fc2 to your computer and use it in GitHub Desktop.
Gameloop in Swift
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 | |
class GameLoop : NSObject { | |
var doSomething: () -> ()! | |
var displayLink : CADisplayLink! | |
var frameInterval : Int! | |
init(frameInterval: Int, doSomething: () -> ()) { | |
self.doSomething = doSomething | |
self.frameInterval = frameInterval | |
super.init() | |
start() | |
} | |
// you could overwrite this too | |
func handleTimer() { | |
doSomething() | |
} | |
func start() { | |
displayLink = CADisplayLink(target: self, selector: Selector("handleTimer")) | |
displayLink.frameInterval = frameInterval | |
displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes) | |
} | |
func stop() { | |
displayLink.removeFromRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode) | |
displayLink = nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment