Skip to content

Commit

Permalink
Use a dummy animation object to deal with the completion callback, fi…
Browse files Browse the repository at this point in the history
…xing 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 be6971f
Showing 1 changed file with 23 additions and 41 deletions.
64 changes: 23 additions & 41 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,30 @@ 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")
} else {
didFinishSettingHiddenValue(view, hidden: hidden)
}
}
}

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 hidingAnimation = view.layer.animationForKey("bounds.size")

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 hidingAnimation = hidingAnimation {
let group = CAAnimationGroup()
group.animations = []
group.delegate = TZFuncAnimationDelegate { _ in
animationFinishFunc()
}
group.tz_copyTiming(hidingAnimation)

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

0 comments on commit be6971f

Please sign in to comment.