简体   繁体   English

SwiftUI - 隐藏视图时的动画

[英]SwiftUI - Animation when hiding view

I am trying to figure out an animation with SwiftUI when showing or hiding a view in a group of a body in a view.在视图中的一组主体中显示或隐藏视图时,我试图用 SwiftUI 找出动画。 I have this code:我有这个代码:

    var body: some View {

        Group {

            if isIntroShown {
                EAIntroViewContentView()
                .transition(AnyTransition.opacity.animation(.easeInOut(duration: 1.0)))
            }

            if mainhomeMode == .mylists {
                MyLists()
                    .onReceive(publisher) { (payload) in
                        self.toggleMainView()
                    }
            } else {
                CarsHome()
                    .onReceive(publisher) { (payload) in
                        self.toggleMainView()
                    }
            }

        }.onReceive(publisherIntro) { (payload) in
            self.onShowIntroButton()
        }
    }

When hiding EAIntroView , the transition animation works properly, but the block pops moving up the mainhomeMode to the top of the window without animations.隐藏EAIntroView ,过渡动画正常工作,但块弹出将mainhomeMode移动到窗口顶部而没有动画。 How can I hide and show the Intro view making the hide/show event smooth?如何隐藏和显示介绍视图使隐藏/显示事件平滑?

Well, after some tests and the reply of @Boris I have figured out what I need to do.好吧,经过一些测试和@Boris 的回复,我知道我需要做什么。

The code should be like this:代码应该是这样的:

    func onShowIntroButton() {
        withAnimation(.easeInOut(duration: 0.5)) {
             isIntroShown.toggle()
        }
    }

    var body: some View {

            VStack{                    
                if isIntroShown {
                    EAIntroViewContentView()
                    .transition(AnyTransition.opacity.animation(.linear(duration: 0.5)))
                }

                Spacer()

                if mainhomeMode == .mylists {
                    MyLists()
                        .onReceive(publisher) { (payload) in
                            self.toggleMainView()
                        }

                } else {
                    CarsHome()
                        .onReceive(publisher) { (payload) in
                            self.toggleMainView()
                        }
                }

        }.onReceive(publisherIntro) { (payload) in
            self.onShowIntroButton()
        }
    }

I am toggling views with notifications.我正在使用通知切换视图。 So the animation must be called from the toggle() function.所以动画必须从toggle()函数中调用。

In this case I have to call the animation when the Intro view is toggled in the function onShowIntroButton() .在这种情况下,我必须在onShowIntroButton()函数中切换介绍视图时调用动画。

The VStak and the Spacer() also made the animation smoother.Hope it helps to other devs. VStakSpacer()也使动画更流畅。希望对其他开发者有所帮助。

Have you tried anything like this?你有没有尝试过这样的事情?

withAnimation {
    self.toggleMainView()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM