Skip to content

Commit

Permalink
FIX: remove tween and target from internal cache when kill() is called
Browse files Browse the repository at this point in the history
  • Loading branch information
u10int committed Mar 25, 2016
1 parent 73679b3 commit 5ed0e31
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
6 changes: 0 additions & 6 deletions Pod/Classes/Kinetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@ import UIKit
public func to(item: NSObject, duration: CFTimeInterval, options: [Property]) -> Tween {
let tween = Tween(target: item, from: nil, to: options, mode: .To)
tween.duration = duration
TweenManager.sharedInstance.cache(tween, target: item)

return tween
}

public func from(item: NSObject, duration: CFTimeInterval, options: [Property]) -> Tween {
let tween = Tween(target: item, from: options, to: nil, mode: .From)
tween.duration = duration
TweenManager.sharedInstance.cache(tween, target: item)

return tween
}

public func fromTo(item: NSObject, duration: CFTimeInterval, from: [Property], to: [Property]) -> Tween {
let tween = Tween(target: item, from: from, to: to, mode: .FromTo)
tween.duration = duration
TweenManager.sharedInstance.cache(tween, target: item)

return tween
}
Expand Down Expand Up @@ -68,7 +65,6 @@ public func staggerTo(items: [NSObject], duration: CFTimeInterval, options: [Pro
let tween = Tween(target: item, from: nil, to: options, mode: .To)
tween.duration = duration
timeline.add(tween, position: stagger * CFTimeInterval(idx))
TweenManager.sharedInstance.cache(tween, target: item)
}

return timeline
Expand All @@ -81,7 +77,6 @@ public func staggerFrom(items: [NSObject], duration: CFTimeInterval, options: [P
let tween = Tween(target: item, from: options, to: nil, mode: .To)
tween.duration = duration
timeline.add(tween, position: stagger * CFTimeInterval(idx))
TweenManager.sharedInstance.cache(tween, target: item)
}

return timeline
Expand All @@ -94,7 +89,6 @@ public func staggerFromTo(items: [NSObject], duration: CFTimeInterval, from: [Pr
let tween = Tween(target: item, from: from, to: to, mode: .FromTo)
tween.duration = duration
timeline.add(tween, position: stagger * CFTimeInterval(idx))
TweenManager.sharedInstance.cache(tween, target: item)
}

return timeline
Expand Down
8 changes: 8 additions & 0 deletions Pod/Classes/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public class Timeline: Animation {
add(tweens, position: 0, align: align)
}

deinit {
kill()
}

// MARK: Public Methods

public func add(tween: Tween) -> Timeline {
Expand Down Expand Up @@ -336,7 +340,11 @@ public class Timeline: Animation {

override public func kill() {
super.kill()

TweenManager.sharedInstance.remove(self)
for tween in tweens {
tween.kill()
}
}

// MARK: Internal Methods
Expand Down
13 changes: 13 additions & 0 deletions Pod/Classes/Tween.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ public class Tween: Animation, TweenType {
self.tweenObject = TweenObject(target: target)
super.init()

TweenManager.sharedInstance.cache(self, target: target)
prepare(from: from, to: to, mode: mode)
}

deinit {
kill()
}

// MARK: Animation Overrides

override public func delay(delay: CFTimeInterval) -> Tween {
Expand Down Expand Up @@ -118,7 +123,11 @@ public class Tween: Animation, TweenType {

override public func kill() {
super.kill()

TweenManager.sharedInstance.remove(self)
if let target = target {
TweenManager.sharedInstance.removeFromCache(self, target: target)
}
}

// MARK: Public Methods
Expand Down Expand Up @@ -171,6 +180,10 @@ public class Tween: Animation, TweenType {

super.play()

if let target = target {
TweenManager.sharedInstance.cache(self, target: target)
}

// properties must be sorted so that the first in the array is the transform property, if exists
// so that each property afterwards isn't set with a transform in place
for prop in TweenUtils.sortProperties(properties).reverse() {
Expand Down
15 changes: 14 additions & 1 deletion Pod/Classes/TweenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,20 @@ public class TweenManager {
if tweenCache[target] == nil {
tweenCache[target] = [Tween]()
}
tweenCache[target]?.append(tween)
if let tweens = tweenCache[target] where tweens.contains(tween) == false {
tweenCache[target]?.append(tween)
}
}

func removeFromCache(tween: Tween, target: NSObject) {
if let index = tweenCache[target]?.indexOf(tween) {
tweenCache[target]?.removeAtIndex(index)
}

// remove object reference if all tweens have been removed from cache
if tweenCache[target]?.count == 0 {
removeFromCache(target)
}
}

func removeFromCache(target: NSObject) {
Expand Down

0 comments on commit 5ed0e31

Please sign in to comment.