Last active
October 27, 2022 20:07
-
-
Save borisdipner/1bfbfc55058b0f898ff38cc71b7e52b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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