简体   繁体   中英

SwiftUI - Transition Animations not working in GeoReader?

I have code like this:

            VStack{
                if (DeleteScreen.sharedInstance.postDelete){
                    VStack{
                        GeometryReader{_ in
                            DeletePostAlert()
                                .frame(maxWidth: .infinity, alignment: .center)
                                .frame(maxHeight: .infinity, alignment: .center)
                                .transition(AnyTransition.opacity.animation(.easeInOut(duration: 0.2)))
                            
                        }.background(Color.black.opacity(0.65))
                        
                        .onDisappear{

                        }
                        .onAppear{
                            
                            
                            
                        }
                    }
                }
            }

where the DeletePost alert is defined like this:

class DeleteScreen: ObservableObject {
    static let sharedInstance = DeleteScreen()
    @Published var postDelete: Bool = false
}

However, for some reason, the transition animations are not working. I'm trying to get the DeletePostAlert() to fade in or slide in or something but so far everything I've tried has not worked. I've also tried something like.animation(.spring()), but to no avail.

Any guidance as to what I'm doing wrong here would be appreciated.

Thanks

I have exactly the same problem. Looks like a bug.

A possible solution that could be working for you is to set transition modifier on top of GeometryReader .

VStack {
    if(DeleteScreen.sharedInstance.postDelete) {
        VStack {
            GeometryReader { _ in
                DeletePostAlert()
                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            }
            .transition(AnyTransition.opacity.animation(.easeInOut(duration: 0.2)))
            .background(Color.black.opacity(0.65))
            .onDisappear { ... }
            .onAppear { ... }
        }
    }
}

btw: I'm wondering why are you even using GeometryReader if you ignore its geometry value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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