Latest 0.1.0
License MIT
Platforms ios 9.0
Frameworks UIKit


SBRunLoop is a wrapper for CADisplayLink that allows for easy creation of run loops with either a variable, fixed, or semi-fixed timestep.

WTF is a timestep?


Create an instance of SBRunLoop, you will want to reatin it so that it doesn’t become deallocated. Pass in the callback functions that you want to use.

Complete implementation of a run loop in a view controller:

class ViewController: UIViewController {

    var runLoop: SBRunLoop!

    override func viewDidLoad() {

         runLoop = SBRunLoop(mode: .variable)
         runLoop.update = update(dt:)

    func update(dt: CFTimeInterval) {
         // This method will be called every frame



The update function will be called once each frame. dt will be equal to the amount of time that it took to render the last frame.

SBRunLoop(mode: .variable)


The update function will be called only in increments of the provided step size, and any left over time will be carried over to the next frame. Update may be alled multiple times per frame. For instance, for 120hz update, pass in 1/120. If the device is able to render at 60fps, this update will be called on average twice per frame. dt will always be the fixed step size.

SBRunLoop(mode: .fixed(timeStep: 1/120))

Semi Fixed

The update function will be called in the increments provided, and the once again with the remainder. For instance, if there is 0.1s to render, and the step size is 0.03s, update will be called with dt 0.03, 0.03, 0.03, 0.01. With this time step, there is not time carried over to the next frame.

.semiFixed(timeStep: 1/120)


Pre Update: Called Once before update occurs. dt is always complete frame time.

public var preUpdate: (CFTimeInterval) -> () = {_ in}

Update: Called once or multiple times. dt according to time step.

public var update: (CFTimeInterval) -> () = {_ in}

Post Update: Called Once after update occurs. dt is always complete frame time.

public var postUpdate: (CFTimeInterval) -> () = {_ in}


SBRunLoop is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "SBRunLoop"


Steve Barnegren, [email protected]


SBRunLoop is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "SBRunLoop",
    "version": "0.1.0",
    "summary": "CADisplayLink wrapper",
    "description": "Wrapper for CADisplayLink for easily managing loops with variable or fixed time steps.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "[email protected]": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.1.0"
    "social_media_url": "",
    "platforms": {
        "ios": "9.0"
    "source_files": "SBRunLoop/SBRunLoop/**/*.swift",
    "frameworks": "UIKit",
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This