Skip to content

Instantly share code, notes, and snippets.

@borisdipner
Last active October 27, 2022 20:07
Show Gist options
  • Save borisdipner/1bfbfc55058b0f898ff38cc71b7e52b7 to your computer and use it in GitHub Desktop.
Save borisdipner/1bfbfc55058b0f898ff38cc71b7e52b7 to your computer and use it in GitHub Desktop.
struct AdaptivePagingScrollView: View {
/// { * * * States * * * }
// 01
private func countOffset(for pageIndex: Int) -> CGFloat {
let activePageOffset = CGFloat(pageIndex) * (itemWidth + itemPadding)
return leadingOffset - activePageOffset
}
// 02
private func countPageIndex(for offset: CGFloat) -> Int {
guard itemsAmount > 0 else { return 0 }
let offset = countLogicalOffset(offset)
let floatIndex = (offset)/(itemWidth + itemPadding)
var index = Int(round(floatIndex))
if max(index, 0) > itemsAmount {
index = itemsAmount
}
return min(max(index, 0), itemsAmount - 1)
}
// 03
private func countCurrentScrollOffset() -> CGFloat {
return countOffset(for: currentPageIndex) + gestureDragOffset
}
// 04
private func countLogicalOffset(_ trueOffset: CGFloat) -> CGFloat {
return (trueOffset-leadingOffset) * -1.0
}
/// { * * * Body * * * }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment