Skip to content

Instantly share code, notes, and snippets.

@acrookston
Created October 17, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrookston/ab2b3dcaf67d6bfcf71168f8057916aa to your computer and use it in GitHub Desktop.
Save acrookston/ab2b3dcaf67d6bfcf71168f8057916aa to your computer and use it in GitHub Desktop.
extension UICollectionView {
func indexPathsForCloseItems(offset: Int) -> [IndexPath] {
var indexes = indexPathsForVisibleItems.sorted(by: { $0.row < $1.row })
for x in indexes {
print("had: \(x)")
}
if let index = indexes.last {
for ix in ((index.row + 1)...(index.row + 11)) {
let x = createIndexPath(row: ix, section: index.section)
indexes.append(x)
print("adding: \(x)")
}
}
if let index = indexes.first {
for ix in ((index.row - 11)...(index.row - 1)) {
let x = createIndexPath(row: ix, section: index.section)
indexes.append(x)
print("adding: \(x)")
}
}
return indexes
}
func createIndexPath(row: Int, section: Int) -> IndexPath {
let totalRows = numberOfItems(inSection: section)
if row > totalRows {
return createIndexPath(row: row - totalRows, section: section + 1)
}
if row < 0 {
let prevSectionRows = numberOfItems(inSection: section - 1)
return createIndexPath(row: prevSectionRows - abs(row), section: section - 1)
}
return IndexPath(item: row, section: section)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment