我可以在UIScrollView中使用UIRefreshControl吗?

Sir*_*III 73 iphone uiscrollview xamarin.ios ios uirefreshcontrol

UIScrollView我的应用程序中已有大约5个已加载多个.xib文件.我们现在想用一个UIRefreshControl.它们构建为与UITableViewControllers一起使用(根据UIRefreshControl类引用).我不想重新做所有5个UIScrollView工作.我已经尝试过使用UIRefreshControlUIScrollView的,除了一些东西之外,它按预期工作.

  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];
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以节省一大堆时间并使用UIRefreshControla UIScrollView

谢谢!!!

Pad*_*215 93

我得到了一个UIRefreshControl工作UIScrollView:

- (void)viewDidLoad
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
    scrollView.userInteractionEnabled = TRUE;
    scrollView.scrollEnabled = TRUE;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(500, 1000);

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(testRefresh:) forControlEvents:UIControlEventValueChanged];
    [scrollView addSubview:refreshControl];

    [self.view addSubview:scrollView];
}

- (void)testRefresh:(UIRefreshControl *)refreshControl
{    
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [NSThread sleepForTimeInterval:3];//for 3 seconds, prevent scrollview from bouncing back down (which would cover up the refresh view immediately and stop the user from even seeing the refresh text / animation)

        dispatch_async(dispatch_get_main_queue(), ^{
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"MMM d, h:mm a"];
            NSString *lastUpdate = [NSString stringWithFormat:@"Last updated on %@", [formatter stringFromDate:[NSDate date]]];

            refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdate];

            [refreshControl endRefreshing];

            NSLog(@"refresh end");
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

需要在单独的线程上进行数据更新,否则它将锁定主线程(UI用于更新UI).因此,当主线程忙于更新数据时,UI也会被锁定或冻结,您永远不会看到平滑的动画或微调器.

编辑:好的,我正在做与OP相同的事情,我现在已经添加了一些文本(即"拉到刷新"),它确实需要回到主线程来更新该文本.

更新的答案.

  • 您可能希望将`endRefreshing`放在主队列块中,因为它是一个UI方法. (4认同)
  • @ jsetting32 - 我用这行代码解决了我的问题:[refreshControl.superview sendSubviewToBack:refreshControl]; (2认同)

Die*_*ata 40

添加到上面的答案,在某些情况下你不能设置contentSize(可能使用自动布局?)或者contentSize的高度小于或等于UIScrollView的高度.在这些情况下,UIRefreshControl将无法工作,因为UIScrollView不会反弹.

要解决此问题,请将属性alwaysBounceVertical设置为TRUE.


Ben*_*ard 14

如果当你足够幸运,配套的iOS 10+,你现在可以简单地设置refreshControlUIScrollView.这种工作方式相同,之前存在refreshControlUITableView.


小智 10

以下是在C#/ Monotouch中执行此操作的方法.我无法在任何地方找到C#的任何样本,所以这里是..谢谢Log139!

public override void ViewDidLoad ()
{
    //Create a scrollview object
    UIScrollView MainScrollView = new UIScrollView(new RectangleF (0, 0, 500, 600)); 

    //set the content size bigger so that it will bounce
    MainScrollView.ContentSize = new SizeF(500,650);

    // initialise and set the refresh class variable 
    refresh = new UIRefreshControl();
    refresh.AddTarget(RefreshEventHandler,UIControlEvent.ValueChanged);
    MainScrollView.AddSubview (refresh);
}

private void RefreshEventHandler (object obj, EventArgs args)
{
    System.Threading.ThreadPool.QueueUserWorkItem ((callback) => {  
        InvokeOnMainThread (delegate() {
        System.Threading.Thread.Sleep (3000);             
                refresh.EndRefreshing ();
        });
    });
}
Run Code Online (Sandbox Code Playgroud)


Mac*_*ark 10

由于iOS 10 UIScrollView已经具有refreshControl属性。当您创建UIRefereshControl并将其分配给此属性时,将显示此refreshControl。

没有必要添加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()
   }
}
Run Code Online (Sandbox Code Playgroud)

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

来自https://developer.apple.com/documentation/uikit/uirefreshcontrol的代码和报价