gpt4 book ai didi

ios - 我可以在 UIScrollView 中使用 UIRefreshControl 吗?

转载 作者:IT王子 更新时间:2023-10-29 07:32:23 26 4
gpt4 key购买 nike

我的应用程序中已经有大约 5 个 UIScrollView,它们都加载多个 .xib 文件。我们现在要使用 UIRefreshControl。它们被构建为与 UITableViewControllers 一起使用(根据 UIRefreshControl 类引用)。我不想重做所有 5 个 UIScrollView 的工作方式。我已经尝试在我的 UIScrollView 中使用 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

谢谢!!!

最佳答案

我有一个 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 也被锁定或卡住,您永远看不到流畅的动画或微调器。

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

更新的答案。

关于ios - 我可以在 UIScrollView 中使用 UIRefreshControl 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14905382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com