简体   繁体   English

SwiftUI 中的动画无法正常工作

[英]Animation in SwiftUI not working properly

Does someone know why the following is not animation and is not working entirely and how to fix that?有人知道为什么以下不是动画并且不能完全工作以及如何解决这个问题吗? The problem is that some of our animation is not shown in this case.问题是我们的一些动画在这种情况下没有显示。 Maybe there is a workaround or so?也许有一种解决方法? I read articles and so on for hours and hours now... found nothing我读了好几个小时的文章等等……一无所获

What I'm trying to achieve in the end is:我最终想要实现的是:

  1. The SignUpLogin screen has both .move(edge: .top) and .opacity animations when appearing and disappearing SignUpLogin 屏幕在出现消失时都有.move(edge: .top) .opacity .move(edge: .top).opacity动画

  2. Logout screen has both: .blur and .opacity animation when appearing and disappearing.注销屏幕在出现和消失时都有: .blur.opacity动画。

Blur and Opacity stop working when the views are reappearing (but they work when disappearing, see gif).当视图重新出现时,模糊和不透明度停止工作(但它们在消失时工作,见 gif)。

Xcode 12, iOS 14 Xcode 12,iOS 14 在此处输入图片说明

import SwiftUI
import Foundation
import Combine

class ViewRouter: ObservableObject {
    @Published var signUpLoginView_active: Bool = true
    @Published var newUserView_active: Bool = false
}
struct ContentView: View {
    @EnvironmentObject var viewRouter: ViewRouter
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        ZStack {
            if colorScheme == .dark {
                Color.white.ignoresSafeArea()
            } else {
                Color.black.ignoresSafeArea()
            }
            if viewRouter.newUserView_active {
                NewUserView()
                    .transition(.opacity)
                    .zIndex(4)
            }
            if viewRouter.signUpLoginView_active {
                SignUpLoginView()
                    .transition(.move(edge: .top))
                    .zIndex(5)
            }
        }
        .edgesIgnoringSafeArea(.all)
    }
}
struct SignUpLoginView: View {
    @EnvironmentObject var viewRouter: ViewRouter
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        ZStack() {
            colorScheme == .dark ? Color.black.ignoresSafeArea().zIndex(3) : Color.white.ignoresSafeArea().zIndex(3)
            if colorScheme == .dark {
                Color.white.ignoresSafeArea()
                    .opacity(viewRouter.signUpLoginView_active ? 0 : 1)
                    .zIndex(4)
            } else {
                Color.black.ignoresSafeArea()
                    .opacity(viewRouter.signUpLoginView_active ? 0 : 1)
                    .zIndex(4)
            }
//
            Button("Login") {
                withAnimation(.easeInOut(duration: 3)) {
                    viewRouter.signUpLoginView_active.toggle()
                    viewRouter.newUserView_active.toggle()
                }
            }.zIndex(3)
        }
    }
}
struct NewUserView: View {
    @EnvironmentObject var viewRouter: ViewRouter
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        ZStack {
            TabView {
                Button("Logout") {
                    withAnimation(.easeInOut(duration: 3)) {
                        viewRouter.signUpLoginView_active.toggle()
                        viewRouter.newUserView_active.toggle()
                    }
                }
                  .tabItem {
                     Image(systemName: "person.crop.circle.fill")
                     Text("My Profile")
                  }
            }
        }
        .blur(radius: (viewRouter.signUpLoginView_active ? 3 : 0), opaque: true)
        .padding(viewRouter.signUpLoginView_active ? -20 : 0)
    }
}

Still not clear what is what (because you talk about Login, but in code First/Second), but the following might be helpful for how operate with transitions between views.仍然不清楚什么是什么(因为您谈论的是登录,但在代码 First/Second 中),但以下内容可能对如何操作视图之间的转换有所帮助。 (of course you can use any other transitions or combinations of them). (当然,您可以使用任何其他过渡或它们的组合)。

Tested with Xcode 12 / iOS 14使用 Xcode 12 / iOS 14 测试

struct First: View {
    @EnvironmentObject var viewRouter: ViewRouter
    var body: some View {
        ZStack {
            Color.red
            Button("First View") {
               viewRouter.signUpLoginView_active.toggle()
            }
        }
    }
}

struct Second: View {
    @EnvironmentObject var viewRouter: ViewRouter
    var body: some View {
        ZStack {
            Color.orange
            Button("Second View") {
               viewRouter.signUpLoginView_active.toggle()
            }
        }

    }
}

struct ContentView: View {
    @EnvironmentObject var viewRouter: ViewRouter
    var body: some View {
        ZStack {
        if viewRouter.signUpLoginView_active {
            First()
               .transition(.move(edge: .top))   // transition here
        }
        if !viewRouter.signUpLoginView_active {
            Second()
                .blur(radius: viewRouter.signUpLoginView_active ? 3 : 0)
                .transition(.move(edge: .top)) // and here
        }
        }
        .animation(.easeInOut(duration: 3)) // animation on container
        .edgesIgnoringSafeArea(.all)
    }
}

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

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