UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

PageTabViewStyle() in iOS 14 - how to get current swipe distance/offset?

Forums > SwiftUI

I'm testing out the new tab view style PageTabViewStyle() in iOS 14. I know how to get the current tab index but would like to get the current drag position as well so I can create a custom interpolated animation while the user swipes - one that depends on position of the drag (for example parallax effects or scaling an item the closer it comes on to or away from view.

I've tried attaching drag handlers but they don't seem to work as intended anywhere inside this structure. Any ideas? Guessing there might be a way with GeometryReader


  import SwiftUI
  import PlaygroundSupport

  struct TipsView: View {
    @State private var selected = 1

    var body: some View {
        VStack {
            ZStack {
                TabView(selection: self.$selected) {
                    Text("1")
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.pink)
                        .tag(1)

                    Text("2")
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.red)
                        .tag(2)

                    Text("3")
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.green)
                        .tag(3)

                    Text("4")
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.blue)
                        .tag(4)
                }
                .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
                .tabViewStyle(PageTabViewStyle())
            }
            .background(Color.black)
            .frame(width: 375 - 60, height: 500)

            Text("Current page: \(selected)")
        }
        .background(Color.white)
        .frame(width: 375, height: 812)

    }
  }

  PlaygroundPage.current.setLiveView(TipsView())

4      

Just saw https://www.hackingwithswift.com/plus/remaking-apps/remaking-the-tips-app that does something very similar. Looks like I need to wrap each individual page item in a geometry reader.. will give that a shot

3      

Did you get anywhere with this one @stammy? I'm trying to achieve similar.

3      

I am too

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.