我可以在 UIScrollView 中使用 UIRefreshControl 吗? [英] Can I use a UIRefreshControl in a UIScrollView?

查看:16
本文介绍了我可以在 UIScrollView 中使用 UIRefreshControl 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中已经有大约 5 个 UIScrollView,它们都加载了多个 .xib 文件.我们现在想要使用 UIRefreshControl.它们被构建为与 UITableViewControllers 一起使用(根据 UIRefreshControl 类参考).我不想重新做所有 5 UIScrollView 的工作方式.我已经尝试在我的 UIScrollView 中使用 UIRefreshControl,它按预期工作,除了一些事情.

  1. 刷新图片刚进入加载器后,UIScrollView向下跳了大约10个像素,只有当我非常小心地拖动UIScrollView时才会出现这种情况代码>非常缓慢.

  2. 当我向下滚动并启动重新加载时,然后松开 UIScrollViewUIScrollView 留在我松开的位置.重新加载完成后,UIScrollView 跳到顶部,没有动画.

这是我的代码:

-(void)viewDidLoad{UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];[myScrollView addSubview:refreshControl];}-(void)handleRefresh:(UIRefreshControl *)refresh {//重新加载我的数据[刷新结束刷新];}

有什么办法可以节省大量时间并在 UIScrollView 中使用 UIRefreshControl 吗?

谢谢!!!

解决方案

自从 iOS 10 UIScrollView 已经有一个 refreshControl 属性.当您创建 UIRefereshControl 并将其分配给此属性时,将出现此 refreshControl.

无需将 UIRefereshControl 添加为子视图.

func configureRefreshControl() {//将刷新控件添加到您的 UIScrollView 对象.myScrollingView.refreshControl = UIRefreshControl()myScrollingView.refreshControl?.addTarget(self, action:#selector(handleRefreshControl),对于:.valueChanged)}@objc func handleRefreshControl() {//更新你的内容...//关闭刷新控制.DispatchQueue.main.async {self.myScrollingView.refreshControl?.endRefreshing()}}

<块引用>

UIRefreshControl 对象是您附加到任何 UIScrollView 对象的标准控件

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

I have about 5 UIScrollView's already in my app which all load multiple .xib files. We now want to use a UIRefreshControl. They are built to be used with UITableViewControllers (per UIRefreshControl class reference). I do not want to re-do how all 5 UIScrollView work. I have already tried to use the UIRefreshControl in my UIScrollView's, and it works as expected except for a few things.

  1. Just after the refresh image turns into the loader, the UIScrollView jumps down about 10 pixels, which only does not happen when I am very careful to drag the UIScrollview down very slowly.

  2. When I scroll down and initiate the reload, then let go of the UIScrollView, the UIScrollView stays where I let it go. After it is finished reloading, the UIScrollView jumps up to the top with no animation.

Here is my code:

-(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];
}

Is there any way I can save a bunch of time and use a UIRefreshControl in a UIScrollView?

Thank You!!!

解决方案

Since iOS 10 UIScrollView already has a refreshControl property. This refreshControl will appear when you create a UIRefereshControl and assign it to this property.

There's no need to add UIRefereshControl as a subview anymore.

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()
   }
}

A UIRefreshControl object is a standard control that you attach to any UIScrollView object

Code and quote from https://developer.apple.com/documentation/uikit/uirefreshcontrol

这篇关于我可以在 UIScrollView 中使用 UIRefreshControl 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆