Designed to be used in a SpriteKit project.
Create an instance of an SKTimer object
let timer = SKTimer()Call the start(_:) method on your new timer passing in the current time from the scene. This is the currentTime parameter from your scenes update method. Usually you'll want to create a property in your scene to hold this value so you'll have access to it from outside your update method:
timer.start(currentTime)In your scenes update method make sure you call the timers update(_:completion:) method passing in the current time. This is how the timer calculates its count:
timer.update(currentTime, timeUp: nil)You can get the current time of the timer from it time property:
print(timer.time)##Example Scene
Here is very simple example scene that starts an SKTimer on a touch:
import SpriteKit
import SKTimer
class GameScene: SKScene {
var currentTime = 0.0
var timer = SKTimer()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer.start(currentTime)
}
override func update(currentTime: CFTimeInterval) {
self.currentTime = currentTime
if timer.on {
print(timer.time)
timer.update(currentTime, timeUp: nil)
}
}
}##Time Limits
You can set optional time limits for your timer and receive a callback when that limit is reached. Create a timer with a limit of 10 seconds like this:
var timer = SKTimer(limit: 10)##Multiplier
The default of the multiplier property is 1.0. You can increase or decrease this to speed up or slowdown the speed of the counter. E.g to create a timer that is twice as fast as real time:
var timer = SKTimer(multiplier: 2.0)SKTimer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SKTimer"KyleGoslan
SKTimer is available under the MIT license. See the LICENSE file for more info.