如何用刷新控制限制拉距?

问题描述:

我正在尝试使用UIRefreshControlUITableView中加载一些数据。我做了,如何用刷新控制限制拉距?

- (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。我怎样才能在短暂的拖动(拉)中刷新?

+1

你为什么要这么做?如果你使用标准的'UITableViewController',增加对“pull-to-refresh”的支持只是'viewDidLoad'中的三行简单代码来设置'UIRefreshControl'。 'scrollViewDidScroll:'方法中不需要任何代码。用于setBackgroundView的 – rmaddy

缩短UIRefreshControl拉距离,你可以试试这个:

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

也许你遇到这个问题,因为你设置了一个错误的方式你刷新控制。不要init您的帧刷新控制。只是做[[alloc] init][new]并把它作为表视图的背景视图。表格视图知道如何处理它。

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

这是你通常如何设置拉来刷新控制。但是,如果这仍然不能满足您的要求,您可以继承UIRefreshControl,这对于这种情况绝对不是最佳解决方案,或者您可以按照tnylee暗示的方式欺骗​​UIScrollView委托方法。但是在scrollViewDidScroll中定制默认行为并不是一个明智的决定。

+0

它以其他方式解决了我的问题 – Logic