ios - SwiftUI - 动态过滤时防止部分在列表中向右飞行/缩放

标签 ios swift swiftui swiftui-list

我最初问了这个问题:
SwiftUI - Dynamic List filtering animation flies to right side when data source is empty
在那里,我有一个 List没有部分。我正在过滤它们,以便它只显示包含 TextField 内的文本的行.解决方案是将所有内容包装在 List 中。在 Section .
不幸的是,我现在需要过滤 Section s。这是我的代码:

struct Group: Identifiable {
    let id = UUID() /// required for the List
    
    var groupName = ""
    var people = [Person]()
}
struct Person: Identifiable {
    let id = UUID() /// required for the List
    
    var name = ""
}
struct ContentView: View {

    @State var searchText = ""

    var groups = [
        Group(groupName: "A People", people: [
            Person(name: "Alex"),
            Person(name: "Ally"),
            Person(name: "Allie")
        ]),
        Group(groupName: "B People", people: [
            Person(name: "Bob")
        ]),
        Group(groupName: "T People", people: [
            Person(name: "Tim"),
            Person(name: "Timothy")
        ])
    ]

    var body: some View {

        VStack {
            TextField("Search here", text: $searchText) /// text field
                .padding()
            
            List {
                ForEach(
                    
                    /// Filter the groups for those that contain searchText
                    groups.filter { group in
                        searchText.isEmpty || group.groupName.localizedStandardContains(searchText)
                    }
                    
                ) { group in
                    Section(header: Text(group.groupName)) {
                        ForEach(group.people) { person in
                            Text(person.name)
                        }
                    }
                    
                }
            }
            .animation(.default) /// apply the animation
        }
    }
}
结果:
filtering sections causes them to fly away
我在 ForEach 中传入一个过滤数组确定Section s。但是,每当该数组更改时,List动画真的很奇怪。 Section s 缩放/飞到右侧,当数组再次包含它们时从左侧返回。 我怎样才能避免这个动画?
如果我删除 .animation(.default) ,它根本没有动画,正如预期的那样。但是,我还是想要动画 .有没有办法淡化更改,或者滑动它们?

最佳答案

解决方案不使用列表。只要您不使用选择和行删除 ScrollView 基本上是相同的。
如果您想将其样式设置为有点像 List 也不是那么难:

struct SearchAnimationExample: View {

    ...

    var body: some View {

        VStack {
            TextField("Search here", text: $searchText) /// text field
                .padding()
            
            ScrollView {
                VStack(spacing: 0) {
                    ForEach(
                        groups.filter { group in
                            searchText.isEmpty || group.groupName.localizedStandardContains(searchText)
                        }
                    ) { group in
                        Section(header: header(title: group.groupName)) {
                            ForEach(group.people) { person in
                                row(for: person)
                                Divider()
                            }
                        }
                        
                    }.transition(.opacity) // Set which transition you would like
                    
                    // Always full width
                    HStack { Spacer() }
                }
            }
            .animation(.default) 
        }
    }
    
    func header(title: String) -> some View {
        HStack {
            Text(title).font(.headline)
            Spacer()
        }
        .padding(.horizontal)
        .background(Color.gray.opacity(0.4))
    }
        
    func row(for person: Person) -> some View {
        HStack {
            Text(person.name)
            Spacer()
        }.padding()
    }
}
看起来与默认列表几乎相同:
enter image description here

关于ios - SwiftUI - 动态过滤时防止部分在列表中向右飞行/缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65149806/

相关文章:

ios - Swift 同时检测屏幕左右两侧的触摸

SwiftUI:按下按钮时的 Action

Swift函数在另一个函数完成后执行

ios - 多个动画 block 不起作用

ios - 确保 ScrollView 和 View 始终具有相同的高度

ios - 我是否可以发布其某些功能和 UI 因过期日期而更改的应用程序?

swift - 延迟显示 UIMapView 直到 map 在 Swift 中完全加载

swiftui - 如何将 Color 绑定(bind)到 .fill()?

ios - 给定一个自定义形状,如何增加尺寸以适应 SwiftUI 中的框架?

ios - 开发人员可以访问的最低级别的官方触摸输入 API 是什么? (苹果手机)