简体   繁体   English

SwiftUI animation 条件视图的转换

[英]SwiftUI animation transition of conditional views

So, i have this little app with a button where i can change the how the main view display its items, with a grid or list.所以,我有这个带有按钮的小应用程序,我可以在其中更改主视图显示其项目的方式,使用网格或列表。 But i would like to add a little animation when it makes its transition from one to another.但是当它从一个过渡到另一个时,我想添加一点 animation。 I messed i little bit trying to add withAnimation on the button, or .transition() inside the views, but nothing seems to do the trick.我试图在按钮上添加withAnimation或在视图中添加 .transition .transition()有点搞砸了,但似乎没有任何效果。 Any tips on how can i achieve this?关于如何实现这一目标的任何提示?

struct FrameworkGridView: View {
    @StateObject var viewModel = FrameworkGridViewModel()
    @Binding var isGrid: Bool
    var body: some View {
        NavigationView {
            Group {
                if viewModel.isGrid {
                    ScrollView {
                        LazyVGrid(columns: viewModel.columns) {
                            ForEach(MockData.frameworks) { framework in
                                FrameworkTitleView(framework: framework, isGrid: $viewModel.isGrid)
                                    .onTapGesture {
                                        viewModel.selectedFramework = framework
                                    }
                            }
                        }
                    }
                    .sheet(isPresented: $viewModel.isShowingDetailView) {
                        DetailView(framework: viewModel.selectedFramework ?? MockData.sampleFramework, isShowingDetailView: $viewModel.isShowingDetailView, isGrid: $viewModel.isGrid)
                    }
                } else {
                    List {
                        ForEach(MockData.frameworks) { framework in
                            NavigationLink(destination: DetailView(framework: framework, isShowingDetailView: $viewModel.isShowingDetailView, isGrid: $viewModel.isGrid)) {       
                                FrameworkTitleView(framework: framework, isGrid: $viewModel.isGrid)
                            }
                        }
                    }
                }
            }
            .toolbar {
                Button {
                    viewModel.isGrid.toggle()
                } label: {
                    Image(systemName: viewModel.isGrid ? "list.dash" : "square.grid.2x2")
                }
                
            }
        }
        
    }
}

[ [在此处输入图像描述 ] ]

I believe you're looking for matchedGeometryEffect .我相信您正在寻找matchedGeometryEffect Check out WWDC 2020: What's New in SwiftUI starting around 19 minutes in. The SwiftUI Lab also has a couple of articles about it.查看WWDC 2020:SwiftUI 中的新增功能,从大约 19 分钟开始。SwiftUI 实验室也有几篇关于它的文章。

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

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