How can I use a drag gesture to shrink a view down to its 'compact' size in SwiftUI?

89 Views Asked by At

I have a View that has similar properties to this and I am trying to create a gesture so the user can drag it down to make it compact:

struct myView: View {
    @State var isCompact = false
    var body: some View {
        Text("")
        Text("")
        if isCompact {
            Text("")
        }
        Image("")
        Text("")
        Button {
            isCompact.toggle()
        } label: {
            Text("button")
        }
    }
}

What would be the best way to make this view draggable in such a way?

I am currently using a gesture like this:

.gesture(DragGesture(minimumDistance: 30)
                .onEnded { value in
                    if !compact.isCompact {
                        if value.translation.height > 0 {
                            withAnimation {
                                compact.isCompact = true
                            }
                        }
                    }
                })

but this will only detect the swipe then toggle the isCompact bool, so it doesn't interactively drag the view

0

There are 0 best solutions below