如何通过刷新控制来限制拉动距离?

时间:2015-10-21 17:50:22

标签: ios objective-c uitableview uirefreshcontrol

我正在尝试使用UITableViewUIRefreshControl中加载一些数据。我做了,

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    // Get the current size of the refresh controller
    CGRect refreshBounds = refreshControl.bounds;

    // Distance the table has been pulled >= 0
    CGFloat pullDistance = MAX(0.0, -self.newsTableView.contentOffset.y);

    // Half the width of the table
    CGFloat midX = self.newsTableView.frame.size.width / 2.0;

    CGFloat spinnerHeight = self.compass_spinner.bounds.size.height;
    CGFloat spinnerHeightHalf = spinnerHeight / 2.0;

    CGFloat spinnerWidth = self.compass_spinner.bounds.size.width;
    CGFloat spinnerWidthHalf = spinnerWidth / 2.0;

    // Set the Y coord of the graphics, based on pull distance
    CGFloat spinnerY = pullDistance / 2.0 - spinnerHeightHalf;

    // Calculate the X coord of the graphics, adjust based on pull ratio
    CGFloat spinnerX = (midX - spinnerWidthHalf);

    // When the compass and spinner overlap, keep them together
    self.isRefreshIconsOverlap = YES;

    // If the graphics have overlapped or we are refreshing, keep them together
    if (self.isRefreshIconsOverlap || refreshControl.isRefreshing) {
        spinnerX = midX - spinnerWidthHalf;
    }

    CGRect spinnerFrame = self.compass_spinner.frame;
    spinnerFrame.origin.x = spinnerX;
    spinnerFrame.origin.y = spinnerY;

    self.compass_spinner.frame = spinnerFrame;

    // Set the encompassing view's frames
    refreshBounds.size.height = pullDistance;

    self.refreshColorView.frame = refreshBounds;
    self.refreshLoadingView.frame = refreshBounds;

    // If we're refreshing and the animation is not playing, then play the animation
    if (refreshControl.isRefreshing && !self.isRefreshAnimating) {
        [self animateRefreshView];
    }
}

使用此代码,我可以将表格视图下拉到屏幕底部。我喜欢的UIRefresControl

 // Initialize Refresh Control
refreshControl = [[UIRefreshControl alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];

// Configure Refresh Control
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

要使用此刷新,我需要像屏幕的1/2一样拖动。我怎样才能在短暂拖动(拉动)中刷新它?

2 个答案:

答案 0 :(得分:3)

要缩短 UIRefreshControl 的拉动距离,您可以试试这个:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < -110 && ![refreshControl isRefreshing]) {
        [refreshControl beginRefreshing];
        //your code for refresh
        [refreshControl endRefreshing];
    }
}

答案 1 :(得分:2)

也许您遇到了这个问题,因为您以错误的方式设置了刷新控件。不要init使用框架进行刷新控制。只需执行[[alloc] init][new]并将其作为表格视图的背景视图。表视图知道如何处理它。

self.myRefreshControl = [UIRefreshControl new];
[self.myRefreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.myTableView setBackgroundView:self.myRefreshControl]; 

这就是你通常设置一个pull来刷新控件的方法。但是,如果这仍然不能满足您的要求,您可以将UIRefreshControl子类化,这绝对不是该案例的最佳解决方案,或者您可以像tnylee建议的那样在UIScrollView Delegate方法中进行操作。但是在scrollViewDidScroll中进行自定义默认行为并不是明智的决定。