繁体   English   中英

如何使用刷新控件限制拉动距离?

[英]How to limit the pulling distance with a refresh control?

我正在尝试使用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。 我如何在短暂的拖动(拉动)中刷新它?

要缩短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 Delegate 方法中进行欺骗 但是在scrollViewDidScroll自定义默认行为并不是一个明智的决定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM