Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple TapTempo class for Swift
class DTTapTempo {
private let timeOutInterval: NSTimeInterval
private let minTaps: Int
private var taps: [NSDate] = []
init(timeOut: NSTimeInterval, minimumTaps: Int) {
timeOutInterval = timeOut
minTaps = minimumTaps
}
func addTap() -> Double? {
let thisTap = NSDate()
if let lastTap = taps.last {
if thisTap.timeIntervalSinceDate(lastTap) > timeOutInterval {
taps.removeAll()
}
}
taps.append(thisTap)
guard taps.count >= minTaps else { return nil }
guard let firstTap = taps.first else { return nil }
let avgIntervals = thisTap.timeIntervalSinceDate(firstTap) / Double(taps.count - 1)
return 60.0 / avgIntervals
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment