swift UIRefreshControl() 下拉刷新

例如对UIwebview添加下拉刷新

声明下拉刷新控件

var refreshControl = UIRefreshControl()

 //webView添加下拉刷新

       

        refreshControl.addTarget(self, action:"refreshPage", forControlEvents: .ValueChanged)

        refreshControl.attributedTitleNSAttributedString(string:"Refresh")

        webview.scrollView.addSubview(refreshControl)//关键代码

注意:UIWebView必须保留拖动效果,默认webview.scrollView.bounces 是true,所以不用刻意设置为true。这里是提醒不要设置为false了

//webview.scrollView.bounces = true

//下拉刷新执行的方法

    func refreshPage() {

        //print("Refreshing...")

        webview.reload()

    }


加载完成停止刷新调用:

refreshControl.endRefreshing()

更多下拉上拉刷新请参考:http://www.wtoutiao.com/p/3famTu3.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SwiftUI 中,可以使用 `List` 或 `ScrollView` 来显示可滚动的列表或视图。为了添加下刷新功能,可以使用 `UIRefreshControl`。 首先,需要创建一个 `UIRefreshControl` 的实例,然后将其添加到 `List` 或 `ScrollView` 中。在用户下列表时,`UIRefreshControl` 会触发一个动作。在该动作中,可以执行更新数据的操作,并结束下刷新。 下面是一个示例代码,演示如何在 SwiftUI 中使用 `UIRefreshControl` 实现下刷新: ```swift import SwiftUI struct ContentView: View { @State private var isRefreshing = false @State private var data = ["Item 1", "Item 2", "Item 3"] var body: some View { List(data, id: \.self) { item in Text(item) } .onPullToRefresh(isRefreshing: $isRefreshing) { // Simulate a network request DispatchQueue.main.asyncAfter(deadline: .now() + 2) { self.data = ["New Item 1", "New Item 2", "New Item 3"] self.isRefreshing = false } } } } extension View { func onPullToRefresh(isRefreshing: Binding<Bool>, action: @escaping () -> Void) -> some View { ModifiedContent(content: self, modifier: PullToRefresh(isRefreshing: isRefreshing, action: action)) } } struct PullToRefresh: ViewModifier { @Binding var isRefreshing: Bool var action: () -> Void func body(content: Content) -> some View { content .overlay( GeometryReader { geometry in VStack { if self.isRefreshing { ActivityIndicator(isAnimating: .constant(true), style: .medium) } else { Color.clear } } .frame(width: geometry.size.width, height: 60, alignment: .center) .offset(y: -60) }, alignment: .bottom ) .onAppear { let refreshControl = UIRefreshControl() refreshControl.addTarget(self, action: #selector(self.onRefresh), for: .valueChanged) UITableView.appearance().refreshControl = refreshControl } } @objc private func onRefresh() { isRefreshing = true action() } } struct ActivityIndicator: UIViewRepresentable { @Binding var isAnimating: Bool let style: UIActivityIndicatorView.Style func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView { UIActivityIndicatorView(style: style) } func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicator>) { isAnimating ? uiView.startAnimating() : uiView.stopAnimating() } } ``` 在该示例中,我们使用 `List` 显示一个简单的字符串列表。我们添加了一个名为 `onPullToRefresh` 的自定义修饰符,用于在 `List` 上添加下刷新功能。该修饰符接受两个参数:一个布尔绑定,用于控制刷新控件的状态;一个闭包,用于执行更新数据的操作。 在该示例中,我们使用 `GeometryReader` 将一个 `ActivityIndicatorView` 添加到列表头部。当用户下列表时,`UIRefreshControl` 会触发一个动作,在该动作中,我们将布尔绑定设置为 `true`,然后执行更新数据的操作。当数据更新完成后,我们将布尔绑定设置为 `false`,结束下刷新。 请注意,由于 SwiftUI 中的 `List` 是基于 `UITableView` 实现的,因此我们需要使用 `UITableView` 的 `refreshControl` 属性来添加 `UIRefreshControl`。在 `onAppear` 中,我们设置了 `UITableView` 的 `refreshControl` 属性,将其与我们创建的 `UIRefreshControl` 实例关联起来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值