ios - UIRefreshControl加到UIScrollView上下拉刷新,出现跳跃
PHPz
PHPz 2017-04-17 13:17:45
[iOS讨论组]

一个UIViewController 的- (void)viewDidLoad中初始化了一个UIScrollView在它上面加一个UIRefreshControl,实现下拉刷新。下拉刷新的过程中ScrollView会出现跳跃。谁知道这是为什么?(请回答中不要告诉我可以采用其他方法如EGO,我就是想知道为什么,谢谢大神)
下面是viewDidLoad的代码。

 - (void)viewDidLoad
      {
       [super viewDidLoad];
       UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [scrollView setBackgroundColor:[UIColor whiteColor]];
    [scrollView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:scrollView];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, -200, scrollView.bounds.size.width, 200)];
    [topView setBackgroundColor:[UIColor brownColor]];
    [scrollView addSubview:topView];
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(testRefresh:) forControlEvents:UIControlEventValueChanged];

    [scrollView addSubview:refreshControl];
    [self.view addSubview:scrollView];
    }
PHPz
PHPz

学习是最好的投资!

全部回复(3)
巴扎黑

是因为contentOffset改变导致的,再设置一下contentOffset应该就行了

dispatch_async(dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:kAnimationDuration animations:^{
                self.scrollView.contentInset = inset;
                [self.scrollView setContentOffset:CGPointMake(0, -inset.top) animated:NO];
            } completion:^(BOOL finished) {
            }];
        });
PHP中文网

Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behavior.

我没有试过你这样来用UIRefreshControl。同时,苹果文档已经指出,UIRefreshControl是为了tableView而设计的。所以,会有一些不明确的行为就如上所说。
如果是我,我的解决办法是将UIScrollView改称一个static的UITableView with one cell only, 然后那个cell的高度和你屏幕的高度一样。

多查查文档

阿神

这个问题解决了吗?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号