如何在 SwiftUI 中将视图(即列表)转换为从屏幕上的某个点出现?

发布于 2025-01-11 16:36:06 字数 1494 浏览 0 评论 0原文

我有一个占据屏幕顶部 60% 的列表。我希望列表在转换过程中从 60% 的底部滑入,而不是从手机屏幕的底部滑入,从而实现动画效果。如果你看一下我的代码,你就会明白我在说什么......

struct ContentView: View {
    let controls = ["backward.fill", "play.fill", "forward.fill"]
    
    @State private var showingList = false
    
    var body: some View {
        GeometryReader { geo in
            VStack {
                Group {
                    if showingList {
                        List(0..<50) {
                            Text("\($0)")
                        }
                        .transition(.move(edge: .bottom))
                                                
                    } else {
                        Spacer()
                    }
                }
                .frame(maxWidth: geo.size.width, maxHeight: geo.size.height * 0.6)
//I want the list to slide in from the bottom of the top frame that takes 60% of the height.
                
                HStack {
                    ForEach(controls, id: \.self) {
                        Image(systemName: $0)
                            .foregroundColor(.white)
                    }
                }
                .frame(maxWidth: geo.size.width, maxHeight: geo.size.height * 0.4)
                .onTapGesture {
                    withAnimation {
                        showingList.toggle()
                    }
                }
            }
//I don't want the view to slide in from the bottom of the phone right here
            }
        }
    }

I have a list that occupies 60% of the top of the screen. I want the list to animate by sliding in from the bottom of that 60% during the transition instead of the bottom of the phone screen. If you take a look at my code, you will get an idea on what im talking about...

struct ContentView: View {
    let controls = ["backward.fill", "play.fill", "forward.fill"]
    
    @State private var showingList = false
    
    var body: some View {
        GeometryReader { geo in
            VStack {
                Group {
                    if showingList {
                        List(0..<50) {
                            Text("\($0)")
                        }
                        .transition(.move(edge: .bottom))
                                                
                    } else {
                        Spacer()
                    }
                }
                .frame(maxWidth: geo.size.width, maxHeight: geo.size.height * 0.6)
//I want the list to slide in from the bottom of the top frame that takes 60% of the height.
                
                HStack {
                    ForEach(controls, id: \.self) {
                        Image(systemName: $0)
                            .foregroundColor(.white)
                    }
                }
                .frame(maxWidth: geo.size.width, maxHeight: geo.size.height * 0.4)
                .onTapGesture {
                    withAnimation {
                        showingList.toggle()
                    }
                }
            }
//I don't want the view to slide in from the bottom of the phone right here
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2025-01-18 16:36:06

如果我理解正确的话,您希望列表在出现时隐藏在控件后面。您可以尝试在控件后面添加背景:

            HStack {
                ForEach(controls, id: \.self) {
                    Image(systemName: $0)
                        .foregroundColor(.white)
                }
            }
            .frame(maxWidth: geo.size.width, maxHeight: geo.size.height * 0.4)
            .background(Color(uiColor: .systemBackground)) // <- add a background here to hide the list during transition
            .onTapGesture {
                withAnimation(.linear(duration: 4)) {
                    showingList.toggle()
                }
            }
        }

If I understand you correctly, you want the list to be hidden behind your controls while it appears. You can try adding a background behind the controls:

            HStack {
                ForEach(controls, id: \.self) {
                    Image(systemName: $0)
                        .foregroundColor(.white)
                }
            }
            .frame(maxWidth: geo.size.width, maxHeight: geo.size.height * 0.4)
            .background(Color(uiColor: .systemBackground)) // <- add a background here to hide the list during transition
            .onTapGesture {
                withAnimation(.linear(duration: 4)) {
                    showingList.toggle()
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文