0

I have been trying to achieve paging in swiftUI, which I pretty much achieved using two approaches. But the problem is that the paging Row is inside a vertical scroll. So, the gestures used for paging blocks the vertical scroll.

One of the approaches used for paging is below:

struct SwiftUIPagerView<Content: View & Identifiable>: View {

@Binding var index: Int

@State private var offset: CGFloat = 0
@State private var isGestureActive: Bool = false

// 1
var pages: [Content]

var body: some View {
    GeometryReader { geometry in
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(alignment: .center, spacing: 0) {

                ForEach(self.pages) { page in
                    page
                        .frame(width: geometry.size.width, height: nil)
                }
            }
        }
            // 2
        .content.offset(x: self.isGestureActive ? self.offset : -geometry.size.width * CGFloat(self.index))
        // 3
        .frame(width: geometry.size.width, height: nil, alignment: .leading)
        .simultaneousGesture(DragGesture().onChanged({ value in
            // 4
            self.isGestureActive = true
            // 5
            self.offset = value.translation.width + -geometry.size.width * CGFloat(self.index)
        }).onEnded({ value in
            if -value.predictedEndTranslation.width > geometry.size.width / 2, self.index < self.pages.endIndex - 1 {
                self.index += 1
            }
            if value.predictedEndTranslation.width > geometry.size.width / 2, self.index > 0 {
                self.index -= 1
            }
            // 6
            withAnimation { self.offset = -geometry.size.width * CGFloat(self.index) }
            // 7
            DispatchQueue.main.async { self.isGestureActive = false }
        }))
    }
}
}

Further, I use it like:

            ScrollView(.vertical) {
                SwiftUIPagerView(index: self.$activePageIndex, pages: (0..<4).map {
                    index in HomeBottleCard(bottle: DataSource.getBottles()[0])
                })
                SwiftUIPagerView(index: self.$activePageIndex, pages: (0..<4).map {
                    index in HomeBottleCard(bottle: DataSource.getBottles()[0])
                })
            }

Now the problem arises here. The Vertical scroll does not work. I have tried help from similar questions like Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling

But nothing helped. I would appreciate if anyone could come up with an idea to resolve this issue.

Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32
  • Do some research on this: .highPriorityGesture(YOURGESTURE()) Maybe you can achieve your goal, i don't know how to use it in your context.. Good luck! – Tobias Hesselink Dec 06 '19 at 13:05
  • I have already tried .simulataneousGesture and .gesture without any success. – Amrit Sidhu Dec 06 '19 at 18:12
  • I tried to find a solution for this for months already and think it is not possible. One workaround would be to use a GeometryReader in the Scrollview telling you how far the user dragged it. – leonboe1 Oct 20 '20 at 13:45

0 Answers0