Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
D7239181213E7CF900E0D4E5 /* UITableView+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7239180213E7CF900E0D4E5 /* UITableView+Ext.swift */; };
D7239181213E7CF900E0D4E5 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7239180213E7CF900E0D4E5 /* Extensions.swift */; };
D7239187213E986000E0D4E5 /* Animations.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7239186213E986000E0D4E5 /* Animations.swift */; };
D7239189213EA08700E0D4E5 /* Animator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7239188213EA08700E0D4E5 /* Animator.swift */; };
D7C6B695213E6861002A7BB1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C6B694213E6861002A7BB1 /* AppDelegate.swift */; };
Expand All @@ -18,7 +18,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
D7239180213E7CF900E0D4E5 /* UITableView+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView+Ext.swift"; sourceTree = "<group>"; };
D7239180213E7CF900E0D4E5 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
D7239186213E986000E0D4E5 /* Animations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animations.swift; sourceTree = "<group>"; };
D7239188213EA08700E0D4E5 /* Animator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animator.swift; sourceTree = "<group>"; };
D7C6B691213E6861002A7BB1 /* UITableViewCellAnimation-Article-Final.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "UITableViewCellAnimation-Article-Final.app"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -64,7 +64,7 @@
D7C6B696213E6861002A7BB1 /* TableViewController.swift */,
D7239186213E986000E0D4E5 /* Animations.swift */,
D7239188213EA08700E0D4E5 /* Animator.swift */,
D7239180213E7CF900E0D4E5 /* UITableView+Ext.swift */,
D7239180213E7CF900E0D4E5 /* Extensions.swift */,
D7C6B69B213E6862002A7BB1 /* Assets.xcassets */,
D7C6B6A0213E6862002A7BB1 /* Info.plist */,
D7C6B69D213E6862002A7BB1 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -147,7 +147,7 @@
D7239189213EA08700E0D4E5 /* Animator.swift in Sources */,
D7239187213E986000E0D4E5 /* Animations.swift in Sources */,
D7C6B697213E6861002A7BB1 /* TableViewController.swift in Sources */,
D7239181213E7CF900E0D4E5 /* UITableView+Ext.swift in Sources */,
D7239181213E7CF900E0D4E5 /* Extensions.swift in Sources */,
D7C6B695213E6861002A7BB1 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
104 changes: 52 additions & 52 deletions UITableViewCellAnimation-Article/Animations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,66 @@

import UIKit

typealias Animation = (UITableViewCell, IndexPath, UITableView) -> Void
typealias Animation = (UIView, IndexPath, UIScrollView) -> Void

enum AnimationFactory {

static func makeFade(duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, _ in
cell.alpha = 0
static func makeFade(duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, _ in
cell.alpha = 0

UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
animations: {
cell.alpha = 1
})
}
}
UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
animations: {
cell.alpha = 1
})
}
}

static func makeMoveUpWithBounce(rowHeight: CGFloat, duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, tableView in
cell.transform = CGAffineTransform(translationX: 0, y: rowHeight)
static func makeMoveUpWithBounce(rowHeight: CGFloat, duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, scrollView in
cell.transform = CGAffineTransform(translationX: 0, y: rowHeight)

UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
usingSpringWithDamping: 0.4,
initialSpringVelocity: 0.1,
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
}
UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
usingSpringWithDamping: 0.4,
initialSpringVelocity: 0.1,
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
}

static func makeSlideIn(duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, tableView in
cell.transform = CGAffineTransform(translationX: tableView.bounds.width, y: 0)
static func makeSlideIn(duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, scrollView in
cell.transform = CGAffineTransform(translationX: scrollView.bounds.width, y: 0)

UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
}
UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
}

static func makeMoveUpWithFade(rowHeight: CGFloat, duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, tableView in
cell.transform = CGAffineTransform(translationX: 0, y: rowHeight / 2)
cell.alpha = 0
static func makeMoveUpWithFade(rowHeight: CGFloat, duration: TimeInterval, delayFactor: Double) -> Animation {
return { cell, indexPath, scrollView in
cell.transform = CGAffineTransform(translationX: 0, y: rowHeight / 2)
cell.alpha = 0

UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
cell.alpha = 1
})
}
}
UIView.animate(
withDuration: duration,
delay: delayFactor * Double(indexPath.row),
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
cell.alpha = 1
})
}
}
}
42 changes: 26 additions & 16 deletions UITableViewCellAnimation-Article/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@
import UIKit

final class Animator {
private var hasAnimatedAllCells = false
private let animation: Animation

init(animation: @escaping Animation) {
self.animation = animation
}

func animate(cell: UITableViewCell, at indexPath: IndexPath, in tableView: UITableView) {
guard !hasAnimatedAllCells else {
return
}

animation(cell, indexPath, tableView)

hasAnimatedAllCells = tableView.isLastVisibleCell(at: indexPath)
}
private var hasAnimatedAllCells = false
private let animation: Animation

init(animation: @escaping Animation) {
self.animation = animation
}

func animate(cell: UIView, at indexPath: IndexPath, in scrollView: UIScrollView) {
guard !hasAnimatedAllCells else {
return
}

switch(scrollView) {
case is UICollectionView:
animation(cell, indexPath, scrollView as! UICollectionView)
hasAnimatedAllCells = (scrollView as! UICollectionView).isLastVisibleCell(at: indexPath)
break;
case is UITableView:
animation(cell, indexPath, scrollView as! UITableView)
hasAnimatedAllCells = (scrollView as! UITableView).isLastVisibleCell(at: indexPath)
// Add additional handling for UIScrollView derived types of your choice.
default:
break;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ extension UITableView {
return lastIndexPath == indexPath
}
}

extension UICollectionView {
func isLastVisibleCell(at indexPath: IndexPath) -> Bool {
guard let lastIndexPath = indexPathsForVisibleItems.last else {
return false
}
return lastIndexPath == indexPath
}
}