UITableView拉动刷新导致闪烁。怎么预防呢?

时间:2012-02-27 01:35:23

标签: ios uitableview uikit uiscrollviewdelegate pull-to-refresh

我正在做那种下拉到刷新的事情。在scrollViewDidEndDecelerating中,我检查偏移是否超过某个点,并在scrollViewDidEndDragging中设置contentInset,以便保持下拉部分可见。

然而,这会导致闪烁,可能是由于在滚动动画期间重置了contentInset。我想我可以通过在scrollViewWillEndDragging中设置targetContentOffset来阻止这种情况,但它似乎没有做到这一点。

 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{

     if (scrollView.contentOffset.y < -kRefreshViewDelta) 
     {
         self.tableView.contentInset = UIEdgeInsetsMake(kRefreshViewHeight, 0.0f, 0.0f, 0.0f);

     }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
 {
     if (scrollView.contentOffset.y  < -kRefreshViewDelta) 
     {
          targetContentOffset->y = kRefreshViewHeight ;
     }
 }

3 个答案:

答案 0 :(得分:10)

如果您更改分配给UITableView的帧(在滚动期间或其他情况下),它将导致contentInset重置为默认值(0,0,0,0)。有一些国家检查我的,但基本上这是我为我做的...

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                  willDecelerate:(BOOL)decelerate {
  if (scrollView.contentOffset.y < -kRefreshDeltaY) {
    animation = ^{
        [self setContentInset:UIEdgeInsetsMake(kRefreshDeltaY,
                                                          0, 0, 0)];
    };

    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:animation
                     completion:completion];
  } // if
}

只要我没有布置子视图或更改UITableView的框架,它对我来说就很好。

答案 1 :(得分:4)

迈克的回答对我有用(但我无法对其进行投票或评论)。似乎将我的代码包装在动画块中消除了我的闪烁。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{
    if (scrollView.contentOffset.y < 0 && scrollView.contentInset.top < 0)
    {
        // shows table header view
        // setting inset without using animation block causes flicker
        [UIView animateWithDuration:0.1 animations:^
        {
            scrollView.contentInset = UIEdgeInsetsZero;
        }];
    }

    // hides table header view
    if (scrollView.contentOffset.y > 44 && scrollView.contentInset.top == 0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);
    }
}

答案 2 :(得分:0)

我尝试了一种不同的机制来进行刷新。请检查代码

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (!reloading)
    {
        checkForRefresh = YES;  //  only check offset when dragging
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (reloading) return;

    if (checkForRefresh) {
        if (refreshHeaderView.isFlipped
            && scrollView.contentOffset.y > -65.0f
            && scrollView.contentOffset.y < 0.0f
            && !reloading) {
            [refreshHeaderView flipImageAnimated:YES];
            [refreshHeaderView setStatus:kPullToReloadStatus];


        } else if (!refreshHeaderView.isFlipped
                   && scrollView.contentOffset.y < -65.0f) {
            [refreshHeaderView flipImageAnimated:YES];
            [refreshHeaderView setStatus:kReleaseToReloadStatus];

        }
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
                  willDecelerate:(BOOL)decelerate
{
    if (reloading) return;

    if (scrollView.contentOffset.y <= - 65.0f) {
        if([self.tableView.dataSource respondsToSelector:
            @selector(reloadTableViewDataSource)]){
            [self showReloadAnimationAnimated:YES];
            [self reloadTableViewDataSource];
        }
    }
    checkForRefresh = NO;
}   


- (void)reloadTableViewDataSource
{
    [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];

}