Swift 5 is the basic version, but it does not work so well. I needed to configure it for myself so that it worked.
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { if let tv = scrollView as? UITableView { let path = tv.indexPathForRow(at: targetContentOffset.pointee) if path != nil { self.scrollToRow(at: path!, at: .top, animated: true) } } }
Individual version
// If velocity is less than 0, then scrolling up // If velocity is greater than 0, then scrolling down if let tv = scrollView as? UITableView { let path = tv.indexPathForRow(at: targetContentOffset.pointee) if path != nil { // >= makes scrolling down easier but can have some weird behavior when scrolling up if velocity.y >= 0.0 { // Assumes 1 section // Jump to bottom one because user is scrolling down, and targetContentOffset is the very top of the screen let indexPath = IndexPath(row: path!.row + 1, section: path!.section) if indexPath.row < self.numberOfRows(inSection: path!.section) { self.scrollToRow(at: indexPath, at: .top, animated: true) } } else { self.scrollToRow(at: path!, at: .top, animated: true) } } }
source share