AndreaMiotto / PartialSheet

A SwiftUI Partial Sheet fully customizable with dynamic height

Home Page:https://github.com/AndreaMiotto/PartialSheet/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ScrollView scroll down to drag

tonissimo opened this issue · comments

If the sheet contains a ScrollView, is there a way to drag the sheet down when the user scrolls down?

from https://www.fivestars.blog/articles/scrollview-offset/

I created the ScrollViewOffset view. It handles all logic required to track the offset from the top.

struct ScrollViewOffset<Content: View>: View {
    let content: () -> Content
    let onOffsetChange: (CGFloat) -> Void
  
    init(
        @ViewBuilder content: @escaping () -> Content,
        onOffsetChange: @escaping (CGFloat) -> Void
    ) {
        self.content = content
        self.onOffsetChange = onOffsetChange
    }
  
    var body: some View {
        ScrollView(showsIndicators: false) {
            offsetReader
            content()
        }
            .coordinateSpace(name: "scroll")
            .onPreferenceChange(OffsetPreferenceKey.self, perform: onOffsetChange)
    }
  
    var offsetReader: some View {
        GeometryReader { proxy in
            Color.clear
                .preference(
                key: OffsetPreferenceKey.self,
                value: proxy.frame(in: .named("scroll")).minY
            )
        }
            .frame(height: 0) // 👈🏻 to make sure the frame is hidden!
    }
}

Works perfectly, you can call

ScrollViewOffset {
    //scrollview contents
} onOffsetChange: { offset in
    //offset logic
}
    .onAppear(perform: {
    self.isPresented = true
})
    .onDisappear(perform: {
    self.isPresented = false
})
commented

Check version 3.0 and if you still have the issue, feel free to reopen the issue