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

Animation only for a specific modifier and not others for a same element

Forums > SwiftUI

  • problem better synthesized on my next post *

Hi,

I followed Paul's recommendations (https://www.hackingwithswift.com/books/ios-swiftui/controlling-the-animation-stack), but there is something that must escape me.

i try to apply an animation only for a a modifier among others, but i fail. The code :

       VStack {
           // some code 
        }
        // i don't want animation on these modifiers :
        .frame(width: screen.width)
        .navigationBarTitle("my Title", displayMode: .inline)
        .edgesIgnoringSafeArea(.bottom)
        .animation(.none) // i tried with .animation(nil), it is the same

        // i would like animation on this one only but it doesn't work 😅
        .offset(y: keyboardFocused ? -50 : 0)
        .animation(.easeInOut(duration: 2))

i tried to invert the blocks, but the animation is then active on all modifiers... If someone have an idea !

3      

i progress in my tests. I must specify that the VStack in question is in a navigationView, and that the animation is triggered when the view appears.

if i do this :

VStack {
  // some code
}
.offset(y: keyboardFocused ? -50 : 0)
.animation(.easeInOut(duration: 2))

the animation works for the offset, but also when the view appear, that i don't want.

And if i do this :

VStack {
  // some code
}
.animation(nil)
.offset(y: keyboardFocused ? -50 : 0)
.animation(.easeInOut(duration: 2))

i am good with the fact that the view doesn't animate when appear, but the offset navigation doesn't work anymore.

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.