Skip to content

Instantly share code, notes, and snippets.

@earltedly
Created February 25, 2019 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earltedly/fb10c4d2daec17b83c0e647c968328c4 to your computer and use it in GitHub Desktop.
Save earltedly/fb10c4d2daec17b83c0e647c968328c4 to your computer and use it in GitHub Desktop.
Example implementation of Flicks for macOS/iOS
import Foundation
import CoreMedia
// CMTimeScale is a typealias for Int32
enum Flicks {
static let timescale = CMTimeScale(integerLiteral: 705600000)
}
// CMTime is used throughout AVFoundation & other media APIs
extension CMTime {
init(flicks: CMTimeValue) {
self.init(value: flicks, timescale: Flicks.timescale)
}
var flicks: Int64 {
return convertScale(Flicks.timescale, method: .default).value
}
}
// TimeInterval is a typealias for Double and denotes seconds
// It's used throughout most system / animation code
extension TimeInterval {
init(flicks: Int64) {
self.init(CMTime(flicks: flicks).seconds)
}
var flicks: Int64 {
return CMTime(seconds: self, preferredTimescale: Flicks.timescale).flicks
}
}
// A few tests
CMTime(flicks: 705600000).seconds == 1
CMTime(flicks: 705600000 * 2).seconds == 2
CMTime(seconds: 10, preferredTimescale: 1000).flicks == Int64(705600000) * 10
CMTime(seconds: 20.5, preferredTimescale: 1000).flicks == TimeInterval(20.5).flicks
CMTime(flicks: 234092).seconds == TimeInterval(flicks: 234092)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment