我的应用程序中已经有大约5UIScrollView个,它们都加载了多个.xib文件.我们现在想用UIRefreshControl.它们是为与UITableViewController一起使用而构建的(根据UIRefreshControl类参考).我不想重做所有5UIScrollView的工作方式.在我UIScrollView多岁的时候,我已经try 过使用UIRefreshControl了,除了几件事,它的效果和预期的一样.

  1. 就在刷新图像进入加载程序后,UIScrollView跳下了大约10个像素,这只有在我非常小心地缓慢拖动UIScrollview时才会发生.

  2. 当我向下滚动并开始重新加载时,然后松开UIScrollView,这UIScrollView留在我让它离开的地方.完成重新加载后,UIScrollView跳到顶部,没有动画.

以下是我的代码:

-(void)viewDidLoad
{
      UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
      [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
      [myScrollView addSubview:refreshControl];
}

-(void)handleRefresh:(UIRefreshControl *)refresh {
      // Reload my data
      [refresh endRefreshing];
}

有没有什么方法可以让我省下一大堆时间,在UIScrollView号车上用UIRefreshControl号呢?

谢谢你!

推荐答案

因为iOS 10个UIScrollView已经has a refreshControl property个了.当您创建UIRefereshControl并将其分配给此属性时,将显示此fresh hControl.

no need个可以再将UIRefereshControl添加为子视图.

func configureRefreshControl () {
   // Add the refresh control to your UIScrollView object.
   myScrollingView.refreshControl = UIRefreshControl()
   myScrollingView.refreshControl?.addTarget(self, action:
                                      #selector(handleRefreshControl),
                                      for: .valueChanged)
}

@objc func handleRefreshControl() {
   // Update your content…

   // Dismiss the refresh control.
   DispatchQueue.main.async {
      self.myScrollingView.refreshControl?.endRefreshing()
   }
}

UIRefresh Control对象是附加到任何UIScrollView对象的标准控件

代码和引用自https://developer.apple.com/documentation/uikit/uirefreshcontrol

Ios相关问答推荐

检测touch 并忽略其他手势SWIFT SceneKit

在将Cocoapods更新到1.13.0之后,它抛出错误

安装新版本 XCode 15.0 后无法运行应用程序 XCode

NSError 错误领域: "CBATTErrorDomain" 代码: 128 0x0000000282b111a0

圆角矩形路径上的蛇形动画

在Xcode 15中为具有@Binding的视图创建SwiftUI #预览的方法

不可发送类型 '[String : Any]?'在调用非隔离实例方法 XXX 时退出主参与者隔离上下文不能跨越参与者边界

如何在 SwiftUI 中同时使用 LongPressGesture 和 ScrollView 中的滚动?

在 SwiftUI 中,绑定应该放在 ViewModel 中吗?

SwiftUI 解除 .alert 弹出 NavigationView

帧过渡动画有条件地工作

我们可以在 Swift 中将 struct 中的 Codable 键设置为不区分大小写吗

如何在 Swift 上的 sendSynchronousRequest 中判断 Response.statusCode

何时需要将应用程序源包含在测试目标中?

如何忽略touch 事件并将它们传递给另一个子视图的 UIControl 对象?

当键盘出现在 iOS 中时,将 UIView 上移

如何动态计算 UILabel 高度?

iOS:设备旋转后如何运行函数(Swift)

UIView - 加载视图时如何获得通知?

为什么我需要@1x、@2x 和@3x iOS 图像?