SwiftUI Introspect 库拉取刷新问题

发布于 2025-01-10 23:06:41 字数 916 浏览 0 评论 0原文

我想在 SwiftUi 的滚动视图中实现拉动刷新。所以我一直在尝试 Introspect 库来使用其 .introspectScrollView 和使用刷新控制器。 但我遇到了一个问题,刷新进度旋转器立即隐藏在我的末端。所以我想知道如何继续显示刷新微调器直到数据加载。

另请注意,我没有使用列表,因为 ScrollView 中有子视图,所以我仍在使用 ScrollView。

我的代码是 -

   ScrollView{
     //subView1
     //subView2
   }.introspectScrollView { scrollView in
           let control = UIRefreshControl()
           control.addTarget(viewModel, action: #selector(viewModel.loadData1), for: .valueChanged)
           scrollView.refreshControl = control
           viewModel.isRefreshed = {
                control.endRefreshing()
           }
     }


   //In view model 
   var isRefreshed: (() -> ())?
   func loadData1(){
      // load data 1
      loadData2()
    }
   func loadData2(){
      //load data2
       
      isRefreshed?()
    }

I want to implement pull to refresh in my scrollview in SwiftUi. So I have been trying Introspect library to use its .introspectScrollView and use refresh controller.
But I have been getting an issue with it, the refresh progress spinner immediately gets hidden in my end. So I wanted to know how to keep showing the refresh spinner until the data loads.

Also please note I am not using list as I have subViews inside the ScrollView, so I am still using ScrollView.

My code is -

   ScrollView{
     //subView1
     //subView2
   }.introspectScrollView { scrollView in
           let control = UIRefreshControl()
           control.addTarget(viewModel, action: #selector(viewModel.loadData1), for: .valueChanged)
           scrollView.refreshControl = control
           viewModel.isRefreshed = {
                control.endRefreshing()
           }
     }


   //In view model 
   var isRefreshed: (() -> ())?
   func loadData1(){
      // load data 1
      loadData2()
    }
   func loadData2(){
      //load data2
       
      isRefreshed?()
    }

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

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

发布评论

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

评论(1

靑春怀旧 2025-01-17 23:06:42

您可以尝试 SwiftUIRefresh 包,其中包括 Introspect 和 SwiftuiRefresh 包。因此,将 https://github.com/timbersoftware/SwiftUIRefresh.git 链接添加到您的包中,并使用以下

import SwiftUI
import SwiftUIRefresh

struct ContentView: View {
    
    @State private var isShowing = false
    var body: some View {
        List {
            Text("Item 1")
            Text("Item 2")
        }
        .pullToRefresh(isShowing: $isShowing) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                self.isShowing = false
            }
        }
    }
}

源代码:https://github.com/siteline/SwiftUIRefresh

You could try the SwiftUIRefresh package, that includes both Introspect and SwiftuiRefresh packages. So, add https://github.com/timbersoftware/SwiftUIRefresh.git link to your package and use this like

import SwiftUI
import SwiftUIRefresh

struct ContentView: View {
    
    @State private var isShowing = false
    var body: some View {
        List {
            Text("Item 1")
            Text("Item 2")
        }
        .pullToRefresh(isShowing: $isShowing) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                self.isShowing = false
            }
        }
    }
}

Source code from: https://github.com/siteline/SwiftUIRefresh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文