Skip to content

Commit

Permalink
Make Timeline Array and Dictionary literal convertible
Browse files Browse the repository at this point in the history
  • Loading branch information
modzelewski committed Aug 23, 2016
1 parent 07ef4c8 commit 7b21141
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 39 additions & 6 deletions Sources/timeline/TimeLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions TRXTests/specs/timeline/TimeLineSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -58,7 +58,7 @@ class TimeLineSpec: QuickSpec {
describe("init", {

beforeEach({
subject = TimeLine(tweens: [tweenA, tweenB, tweenC])
subject = [tweenA, tweenB, tweenC]
})

it("should contain tweens") {
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit 7b21141

Please sign in to comment.