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

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

问题描述

我的应用程序中已经有大约5 UIScrollView ,它们都加载了多个 .xib 文件。我们现在想要使用 UIRefreshControl 。它们被构建为与UITableViewControllers一起使用(根据UIRefreshControl类引用)。我不想重新做所有5 UIScrollView 的工作。我已经尝试在我的 UIScrollView 中使用 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. 刷新图像变成装载程序后, UIScrollView 跳下大约10个像素,这只是在我非常小心地将 UIScrollview 拖得很慢时才会发生。

  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.

当我向下滚动并启动重新加载时,放开 UIScrollView UIScrollView 留在我放手的地方。重新加载完成后, UIScrollView 跳到顶部,没有动画。

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.

这是我的代码:

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

有什么方法可以节省大量时间并使用 UIRefreshControl UIScrollView

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

谢谢!!! / p>

Thank You!!!

推荐答案

我得到一个 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");
        });
    });
}

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

Need to do the data update on a separate thread or it will lock up the main thread (which the UI uses to update the UI). So while the main thread is busy updating the data, the UI is also locked up or frozen and you never see the smooth animations or spinner.

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

ok, I'm doing the same thing as OP and i've now added some text to it (ie, "Pull to Refresh") and it does need to get back onto the main thread to update that text.

更新后的答案。

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

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