代码之家  ›  专栏  ›  技术社区  ›  Ryan Heitner

用于展开和折叠的快捷菜单列表动画

  •  0
  • Ryan Heitner  · 技术社区  · 6 年前

    我无法让我的SwiftUI列表为行的展开和折叠设置动画。(Xcode 12.0测试版和模拟器)

    我有几个部分,每个部分都有很多元素。当我点击该部分时,我希望它切换为展开/折叠。

    我不知道应该在哪里指定这个动画修改器。

    import SwiftUI
    
    struct MySection : Identifiable {
        let id = UUID()
        let name:String
    }
    
    let mySections = [MySection(name: "s1"),MySection(name: "s2"),MySection(name: "s3")]
    
    struct Content: View {
        @State var isExpanded : [Bool] = [false,false,false]
        @State var rotation = 0.0
    
        var body: some View {
            List {
                ForEach(mySections.indices) { index in
                    Section(header: Text("\(mySections[index].name)")) {
                        if isExpanded[index] {
                            ForEach(0...4, id: \.self) {
                               Text("Number: \($0)")
                            }
                        } else {
                            EmptyView()
                        }
                    }.onTapGesture {
                        withAnimation {
                            isExpanded[index].toggle()
                        }
                    }
                }
            }
        }
    }
    
    0 回复  |  直到 6 年前
    推荐文章