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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.