Skip to content

Commit

Permalink
Use an dummy animation object to deal with the completion callback, f…
Browse files Browse the repository at this point in the history
…ixing tomvanzummeren#70 (Completion closure never called when animating showing/hiding) and tomvanzummeren#50 (Setting TZStackView to hidden inside animateAlongsideCoordinator's animation block causes strange behavior)
  • Loading branch information
CosynPa committed Mar 5, 2016
1 parent a3f93cc commit 2e0ad5c
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions TZStackView/TZStackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@

import UIKit

struct TZAnimationDidStopQueueEntry: Equatable {
let view: UIView
let hidden: Bool
}

func ==(lhs: TZAnimationDidStopQueueEntry, rhs: TZAnimationDidStopQueueEntry) -> Bool {
return lhs.view === rhs.view
}

public class TZStackView: UIView {

public var distribution: TZStackViewDistribution = .Fill {
Expand Down Expand Up @@ -51,8 +42,6 @@ public class TZStackView: UIView {

private var spacerViews = [UIView]()

private var animationDidStopQueueEntries = [TZAnimationDidStopQueueEntry]()

private var registeredKvoSubviews = [UIView]()

private var animatingToHiddenViews = [UIView]()
Expand Down Expand Up @@ -112,37 +101,40 @@ public class TZStackView: UIView {

removeHiddenListener(view)
view.hidden = false

if let _ = view.layer.animationKeys() {
UIView.setAnimationDelegate(self)
animationDidStopQueueEntries.insert(TZAnimationDidStopQueueEntry(view: view, hidden: hidden), atIndex: 0)
UIView.setAnimationDidStopSelector("hiddenAnimationStopped")

let hiddenAnimation: CAAnimation?
if let animationKeys = view.layer.animationKeys() {
let boundsKey = "bounds.size"
if animationKeys.indexOf(boundsKey) != nil {
hiddenAnimation = view.layer.animationForKey(boundsKey)
} else {
hiddenAnimation = nil
}
} else {
didFinishSettingHiddenValue(view, hidden: hidden)
hiddenAnimation = nil
}
}
}

private func didFinishSettingHiddenValue(arrangedSubview: UIView, hidden: Bool) {
arrangedSubview.hidden = hidden
if let index = animatingToHiddenViews.indexOf(arrangedSubview) {
animatingToHiddenViews.removeAtIndex(index)
}
addHiddenListener(arrangedSubview)
}

func hiddenAnimationStopped() {
var queueEntriesToRemove = [TZAnimationDidStopQueueEntry]()
for entry in animationDidStopQueueEntries {
let view = entry.view
if view.layer.animationKeys() == nil {
didFinishSettingHiddenValue(view, hidden: entry.hidden)
queueEntriesToRemove.append(entry)

let animationFinishFunc = { [weak self, weak view] () in
view?.layer.hidden = hidden
if let selv = self, strongView = view {
if let index = selv.animatingToHiddenViews.indexOf(strongView) {
selv.animatingToHiddenViews.removeAtIndex(index)
}
selv.addHiddenListener(strongView)
}
}
}
for entry in queueEntriesToRemove {
if let index = animationDidStopQueueEntries.indexOf(entry) {
animationDidStopQueueEntries.removeAtIndex(index)

if let hiddenAnimation = hiddenAnimation {
let group = CAAnimationGroup()
group.animations = []
group.delegate = TZFuncAnimationDelegate { _ in
animationFinishFunc()
}
group.tz_copyTiming(hiddenAnimation)

view.layer.addAnimation(group, forKey: "TZSV-hidden-callback")
} else {
animationFinishFunc()
}
}
}
Expand Down

0 comments on commit 2e0ad5c

Please sign in to comment.