Last active
October 27, 2022 20:05
-
-
Save borisdipner/6e289cf6ca7f74af8e1f2412258fc95f 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 { | |
private let items: [AnyView] | |
private let itemPadding: CGFloat | |
private let itemSpacing: CGFloat | |
private let itemWidth: CGFloat | |
private let itemsAmount: Int | |
private let contentWidth: CGFloat | |
private let leadingOffset: CGFloat | |
private let scrollDampingFactor: CGFloat = 0.66 | |
@Binding var currentPageIndex: Int | |
@State private var currentScrollOffset: CGFloat = 0 | |
@State private var gestureDragOffset: CGFloat = 0 | |
init<A: View>(currentPageIndex: Binding<Int>, | |
itemsAmount: Int, | |
itemWidth: CGFloat, | |
itemPadding: CGFloat, | |
pageWidth: CGFloat, | |
@ViewBuilder content: () -> A) { | |
let views = content() | |
self.items = [AnyView(views)] | |
self._currentPageIndex = currentPageIndex | |
self.itemsAmount = itemsAmount | |
self.itemSpacing = itemPadding | |
self.itemWidth = itemWidth | |
self.itemPadding = itemPadding | |
self.contentWidth = (itemWidth+itemPadding)*CGFloat(itemsAmount) | |
let itemRemain = (pageWidth-itemWidth-2*itemPadding)/2 | |
self.leadingOffset = itemRemain + itemPadding | |
} | |
/// { * * * Functions * * * } | |
/// { * * * Body * * * } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment