From 7b211418f35443abd908abfa3dea92fbfc0f39f9 Mon Sep 17 00:00:00 2001 From: Adam Modzelewski Date: Tue, 23 Aug 2016 12:32:29 +0200 Subject: [PATCH] Make Timeline Array and Dictionary literal convertible --- README.md | 10 +++-- Sources/timeline/TimeLine.swift | 45 +++++++++++++++++++--- TRXTests/specs/timeline/TimeLineSpec.swift | 12 +++--- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a5ab5c0..da1e904 100644 --- a/README.md +++ b/README.md @@ -83,12 +83,14 @@ TimeLine(tweens: [ ``` ![Timeline](https://trxy.github.io/TRX/docs/timeline.png) -Add tween with an overlap +Add tweens with overlap ```swift -let timeline = TimeLine() -timeline.add(1.trxTo(20) { object.value = $0 }) -timeline.add(30.trxTo(40) { anOtherObject.value = $0 }, shift: -0.5) +let timeline: TimeLine = [ + 1.trxTo(20) { object.value = $0 }): 0, + 30.trxTo(40) { anOtherObject.value = $0 }: -0.5, + 40.trxTo(50) { anOtherObject.value = $0 }: -0.5 +] timeline.start() ``` ![Timeline with an overlap](https://trxy.github.io/TRX/docs/timeline_overlap.png) diff --git a/Sources/timeline/TimeLine.swift b/Sources/timeline/TimeLine.swift index 1d8b306..946893b 100644 --- a/Sources/timeline/TimeLine.swift +++ b/Sources/timeline/TimeLine.swift @@ -4,7 +4,7 @@ Use a Timelines to chain Tweens or even other Timelines. */ -final public class TimeLine: AbstractTweenable, Tweenable { +final public class TimeLine: AbstractTweenable, Tweenable, ArrayLiteralConvertible, DictionaryLiteralConvertible { //MARK: nested @@ -36,19 +36,50 @@ final public class TimeLine: AbstractTweenable, Tweenable { private var container = [TweenContainer]() var onUpdate: UpdateClosure? - //MARK: initializer + //MARK: initializers /** - Creates a new TimeLine - - Parameter tweens: (optional) An array of Tweenables + Creates new TimeLine + + - Parameter tweens: An array of Tweenables + */ - public init(tweens: [Tweenable]? = nil) { + public init(tweens: [Tweenable]) { super.init() - tweens?.forEach { add($0) } + tweens.forEach { add($0) } moveToStart(); } + /** + + Creates new TimeLine + + */ + public override init() { + super.init() + } + + /** + + Creates new TimeLine from a literal array + + */ + public convenience init(arrayLiteral elements: Tweenable...) { + self.init(tweens: elements) + } + + /** + + Creates new TimeLine from a literal dictionary + + */ + public init(dictionaryLiteral elements: (Tweenable, Double)...) { + super.init() + elements.forEach { add($0.0, shift: $0.1) } + self.moveToStart(); + } + //MARK: lifecycle private func moveToStart() { @@ -100,10 +131,12 @@ final public class TimeLine: AbstractTweenable, Tweenable { } /** + Add a Tweenable to Timeline - Parameter tween: Tweenable - Parameter shift: Delta to previous Tweenable (in seconds); defaults to 0 + */ public func add(tween: Tweenable, shift: NSTimeInterval = 0.0) { let start = duration + shift diff --git a/TRXTests/specs/timeline/TimeLineSpec.swift b/TRXTests/specs/timeline/TimeLineSpec.swift index 778c4e9..5e10521 100644 --- a/TRXTests/specs/timeline/TimeLineSpec.swift +++ b/TRXTests/specs/timeline/TimeLineSpec.swift @@ -42,7 +42,7 @@ class TimeLineSpec: QuickSpec { describe("when initialized with tweens") { beforeEach() { - subject = TimeLine(tweens: [tweenA, tweenB, tweenC]) + subject = [tweenA, tweenB, tweenC] } it("updates first tween to initial position") { @@ -58,7 +58,7 @@ class TimeLineSpec: QuickSpec { describe("init", { beforeEach({ - subject = TimeLine(tweens: [tweenA, tweenB, tweenC]) + subject = [tweenA, tweenB, tweenC] }) it("should contain tweens") { @@ -140,9 +140,11 @@ class TimeLineSpec: QuickSpec { context("shift") { beforeEach({ - subject.add(tweenA, shift: 0.1) - subject.add(tweenB, shift: 0.2) - subject.add(tweenC, shift: -1.5) + subject = [ + tweenA: 0.1, + tweenB: 0.2, + tweenC: -1.5, + ] }) context("on start") {