From 458745531061b485c2ca95d1e50340e530eb7d9e Mon Sep 17 00:00:00 2001 From: Joe Fabisevich Date: Mon, 28 Aug 2017 13:34:04 -0400 Subject: [PATCH] Fixes completion block to fire after all cell animations have completed, not each individual cell --- .../TableFlipExample/TableViewController.swift | 2 +- src/UITableView+TableFlip.swift | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Example/TableFlipExample/TableViewController.swift b/Example/TableFlipExample/TableViewController.swift index ac37e50..dddb661 100644 --- a/Example/TableFlipExample/TableViewController.swift +++ b/Example/TableFlipExample/TableViewController.swift @@ -116,7 +116,7 @@ private extension TableViewController { case .left: let leftAnimation = TableViewAnimation.Cell.left(duration: 0.5) - self.tableView.animateCells(animation: leftAnimation) + self.tableView.animateCells(animation: leftAnimation, indexPaths: nil, completion: nil) case .custom: let degrees = sin(90.0 * CGFloat.pi/180.0) diff --git a/src/UITableView+TableFlip.swift b/src/UITableView+TableFlip.swift index f25e621..36b7c74 100644 --- a/src/UITableView+TableFlip.swift +++ b/src/UITableView+TableFlip.swift @@ -153,6 +153,7 @@ fileprivate extension UITableView { fileprivate extension UITableView { + // Fix this one func animateTableCellsWithDirection(duration: TimeInterval, direction: TableViewAnimation.Cell.AnimationDirection, indexPaths:[IndexPath]?, completion: (() -> Void)? = nil) { let visibleCells: [UITableViewCell] @@ -173,12 +174,16 @@ fileprivate extension UITableView { UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: 0.0, options: .curveEaseInOut, animations: { cell.layer.setAffineTransform(.identity) - }, completion: { finished in - completion?() - }) + }, completion: nil) + } + + let completionDelay: Int = Int((2 * duration)*1000) + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(completionDelay)) { + completion?() } } + // Fix this one func animateTableCellsWithTransform(duration: TimeInterval, transform: CGAffineTransform, options: UIViewAnimationOptions = .curveEaseInOut, completion: (() -> Void)? = nil) { for (index, cell) in self.visibleCells.enumerated() { let delay: TimeInterval = duration/Double(self.visibleCells.count)*Double(index) @@ -188,9 +193,12 @@ fileprivate extension UITableView { UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: 0.0, options: options, animations: { cell.layer.setAffineTransform(.identity) - }, completion: { finished in + }, completion: nil) + + let completionDelay: Int = Int((2 * duration)*1000) + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(completionDelay)) { completion?() - }) + } } }