5

我正在做下拉刷新的事情。在 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 ;
     }
 }
4

3 回答 3

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 的框架,它对我来说表现得很好。

于 2012-03-07T05:55:50.387 回答
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);
    }
}
于 2012-03-28T17:34:02.190 回答
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];

}
于 2012-03-13T04:24:46.247 回答