如何在SwiftUI中实现淡入/淡出搜索字段?

时间:2019-08-21 19:08:55

标签: swiftui ios13

我需要在列表中实现搜索和过滤功能,例如可以从列表中淡出和淡出。 iOS邮件应用程序。当向下拖动到列表的初始位置之外时,将显示搜索/过滤器功能,当向上拖动搜索/过滤器元素时将其隐藏,然后滚动列表。我正在使用Xcode 11 beta 6 / iOS13 beta7。

结构是

struct ContentView: View {
    @State var showFilter: Bool = false
    var filterOptions = ["Opt1", "Opt2", "Opt3"]
    @State private var selectedFilter = 0

    func buildView(name: String) -> AnyView {
        return AnyView (
            Section(header: Text(name)) {
                Text("Some Content")
                Text("Some Content")
                Text("Some Content")
            }
        )
    }
    func filterPicker() -> AnyView {
        if showFilter == true {
            return AnyView (
                Picker(selection: $selectedFilter, label: Text("Filter")) {
                    ForEach(0 ..< filterOptions.count) {
                        Text(self.filterOptions[$0])
                    }}
            )
        } else {
            return AnyView(EmptyView())
        }
    }


    var body: some View {
        TabView {
            NavigationView {
                List {
                    self.filterPicker()
                    Toggle(isOn: self.$showFilter) {
                        Text("toggle filter")
                    }.padding()
                    self.buildView(name: "section1")
                    self.buildView(name: "section2")
                    self.buildView(name: "section3")
                    self.buildView(name: "section4")
                }
                .navigationBarTitle("Devices")
                .listStyle(GroupedListStyle())
                .pickerStyle(DefaultPickerStyle())
            }
            .tag(0)
            .tabItem {
                Image(systemName: "1.square.fill")
                Text("Main")
            }
        }
    }
}

如何检测到将列表向下拖动到阈值以上的动作?

我尝试使用Implementing Pull to Refresh in SwiftUI

中的类似代码
        GeometryReader { g -> Text in
           let frame = g.frame(in: CoordinateSpace.global)
            if frame.origin.y > 250 {
                showFilter = true
                return Text("Filter")
            } else {
                showFilter = false
                print("no filter")
                return Text("")
            }

}

但这会导致类似Xcode的错误

Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

Cannot convert return expression of type 'TabView<Int, TupleView<(some View, some View)>>' to return type 'some View'

关于如何实现这种功能的任何想法吗?

0 个答案:

没有答案