Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

swift - SwiftUI: Can't get the transition of a DetailView to a ZStack in the MainView to work

I can't find the answer to this anywhere, hopefully one of you can help me out.

I have a MainView with some content. And with the press of a button I want to open a DetailView. I am using a ZStack to layer the DetailView on the top, filling the screen.

But with the following code I can't get it to work. The DetailView does not have a transition when it inserts and it stops at removal. I have tried with and without setting the zIndex manually, and a custom assymetricalTransition. Couldn't get that to work. Any solutions?

//MainView
@State var showDetail: Bool = false

var body: some View {
    ZStack {
        VStack {
            Text("Hello MainWorld")
            Button(action: {
                withAnimation(.spring()) {
                    self.showDetail.toggle()
                }
            }) {
                Text("Show detail")
            }
        }


        if showDetail {
            ContentDetail(showDetail: $showDetail)
                .transition(.move(edge: .bottom))
        }
    }
    .edgesIgnoringSafeArea(.all)
}

And here is the DetailView:

//DetailView

@Binding var showDetail: Bool    

var body: some View {
    VStack (spacing: 25) {
        Text("Hello, DetailWorld!")

        Button(action: { withAnimation(.spring()) {
            self.showDetail.toggle()
            }

        }) {
            Text("Close")
        }
        .padding(.bottom, 50)


    }
    .frame(width: UIScreen.main.bounds.width,
           height: UIScreen.main.bounds.height)
    .background(Color.yellow)
    .edgesIgnoringSafeArea(.all)

}

The result of this code is this: enter image description here

I'm running Xcode 11.4.1 so implicit animations doesn't seem to work either. Really stuck here, hope one of you can help me out! Thanks :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a solution. Tested with Xcode 11.4 / iOS 13.4.

demo

struct MainView: View {
    @State var showDetail: Bool = false

    var body: some View {
        ZStack {
            Color.clear // extend ZStack to all area
            VStack {
                Text("Hello MainWorld")
                Button(action: {
                        self.showDetail.toggle()
                }) {
                    Text("Show detail")
                }
            }

            if showDetail {
                DetailView(showDetail: $showDetail)
                    .transition(AnyTransition.move(edge: .bottom))
            }
        }
        .animation(Animation.spring())  // one animation to transitions
        .edgesIgnoringSafeArea(.all)
    }
}

struct DetailView: View {
    @Binding var showDetail: Bool

    var body: some View {
        VStack (spacing: 25) {
            Text("Hello, DetailWorld!")

            Button(action: {
                self.showDetail.toggle()
            }) {
                Text("Close")
            }
            .padding(.bottom, 50)


        }
        .frame(maxWidth: .infinity, maxHeight: .infinity) // fill in container
        .background(Color.yellow)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...