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

SOLVED: Problems with the animations of TabView for swipes vs 'Next' button

Forums > SwiftUI

I've posted the code here: https://stackoverflow.com/questions/76024890/swiftui-tab-view-animation-different-when-swiping-vs-next-button-navigation

But the problem is that I want a Tab View (Page View) to be Swipeable and also navigate via a 'Next' Button (for Onboarding)

The issue is that to get the Swipe to work I have to bundle the 'selected' page inside a 'withAnimation' modifier.

But this then also smooth animates the 'Next' button on the last page (where it becomes the 'Continue' button) = it's a nice effect, but when the user swipes instead of taps the button to advance the page, the button DOES NOT animate between label states and it looks weird.

Is there a way to unify this, either way (no smooth button label animation) or (smooth button label animation on swipe AND tap)?

2      

Hi, You could add an .animation(.default, value: buttonLabel) to the button so it animates on swipe and tap.

            Button(buttonLabel)
            {
              withAnimation
              {
                if self.selectedPage == numberOfPages
                {
                  // Complete the onboarding process - transition view here...

                  return
                }
                selectedPage += 1
              }
            }
            .animation(.default, value: buttonLabel)
            .buttonStyle(.borderedProminent)
            .foregroundColor(.white)

or you can use the .transaction to remove the button animation completely.

            Button(buttonLabel)
            {
              withAnimation
                {
                    if self.selectedPage == numberOfPages
                    {
                        // Complete the onboarding process - transition view here...

                        return
                    }

                    selectedPage += 1
                }
            }
            .transaction { transaction in
                transaction.animation = nil
            }
            .buttonStyle(.borderedProminent)
            .foregroundColor(.white)

your choice.

3      

@Hectorcrdna

Thank you so much!

I haven't got that far in the SwiftUI 100 Days course, so these two modifiers are COMPLETELY new to me, but thank you so much!

2      

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.