Skip to content

Commit

Permalink
Adding support for only animating select index paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mergesort committed Aug 27, 2017
1 parent e4a4049 commit 7580525
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Binary file removed Example/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions Example/TableFlipExample/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum ExampleAnimation {
case top
case left
case custom
case indexPaths

}

Expand Down Expand Up @@ -78,6 +79,9 @@ private extension TableViewController {
case .fade:
self.title = "Fade Animation"

case .indexPaths:
self.title = "Select Index Paths Animation"

}

// Delayed to simulate a network connection, like in the real world, which is where I live.
Expand Down Expand Up @@ -124,6 +128,11 @@ private extension TableViewController {

self.tableView.animateCells(animation: customAnimation, completion: nil)

case .indexPaths:
let evenIndices = (0..<self.dataSource.exampleItems.count).flatMap { return ($0 % 2 == 0) ? IndexPath(row: $0, section: 0) : nil }
let rightAnimation = TableViewAnimation.Cell.right(duration: 0.5)
self.tableView.animateCells(animation: rightAnimation, indexPaths: evenIndices, completion: nil)

}
}

Expand Down
8 changes: 7 additions & 1 deletion Example/TableFlipExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ private extension ViewController {

let customAnimationButton = self.makeButton(title: "Custom Animation", backgroundColor: #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1), selector: #selector(tappedCustomAnimationButton))
self.stackView.addArrangedSubview(customAnimationButton)


let indexPathsAnimationButton = self.makeButton(title: "Index Paths Animation", backgroundColor: #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1), selector: #selector(tappedIndexPathAnimationButton))
self.stackView.addArrangedSubview(indexPathsAnimationButton)

self.setupConstraints()
}

Expand Down Expand Up @@ -94,4 +97,7 @@ private extension ViewController {
self.pushTableViewController(animation: .left)
}

@objc func tappedIndexPathAnimationButton() {
self.pushTableViewController(animation: .indexPaths)
}
}
22 changes: 16 additions & 6 deletions src/UITableView+TableFlip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ public extension UITableView {
}
}

func animateCells(animation: TableViewAnimation.Cell, completion: (() -> Void)? = nil) {
func animateCells(animation: TableViewAnimation.Cell, indexPaths: [IndexPath]? = nil, completion: (() -> Void)? = nil) {
switch animation {

case .left(let duration):
self.animateTableCellsWithDirection(duration: duration, direction: .left, completion: completion)
self.animateTableCellsWithDirection(duration: duration, direction: .left, indexPaths: indexPaths, completion: completion)

case .right(let duration):
self.animateTableCellsWithDirection(duration: duration, direction: .right, completion: completion)
self.animateTableCellsWithDirection(duration: duration, direction: .right, indexPaths: indexPaths, completion: completion)

case .fade(let duration):
self.animateWithFade(duration: duration, consecutively: true, completion: completion)

case .custom(let duration, let transform, let animationOptions):
self.animateTableCellsWithTransform(duration: duration, transform: transform, options: animationOptions, completion: completion)

}
}

Expand Down Expand Up @@ -153,8 +153,18 @@ fileprivate extension UITableView {

fileprivate extension UITableView {

func animateTableCellsWithDirection(duration: TimeInterval, direction: TableViewAnimation.Cell.AnimationDirection, completion: (() -> Void)? = nil) {
for (index, cell) in self.visibleCells.enumerated() {
func animateTableCellsWithDirection(duration: TimeInterval, direction: TableViewAnimation.Cell.AnimationDirection, indexPaths:[IndexPath]?, completion: (() -> Void)? = nil) {

let visibleCells: [UITableViewCell]

if let indexPaths = indexPaths {
let visibleIndexPaths = indexPaths.flatMap({ return (self.indexPathsForVisibleRows?.contains($0) ?? false) ? $0 : nil })
visibleCells = visibleIndexPaths.flatMap { self.cellForRow(at: $0) }
} else {
visibleCells = self.visibleCells
}

for (index, cell) in visibleCells.enumerated() {
let delay: TimeInterval = duration/Double(self.visibleCells.count)*Double(index)
let damping: CGFloat = 0.55

Expand Down

0 comments on commit 7580525

Please sign in to comment.