【问题标题】:How to limit the pulling distance with a refresh control?如何使用刷新控件限制拉动距离?
【发布时间】:2023-03-31 08:43:01
【问题描述】:

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

【问题讨论】:

  • 你为什么要做这一切?如果您使用标准的UITableViewController,添加对“pull-to-refresh”的支持只需在viewDidLoad 中添加3 行简单的代码即可设置UIRefreshControlscrollViewDidScroll: 方法中不需要任何代码。

标签: ios objective-c uitableview uirefreshcontrol


【解决方案1】:

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

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

【讨论】:

  • 这是一个很好的快速解决问题的方法,您仍然会遇到诸如未完成慢拉动画之类的问题,但缺少自定义的东西,这可以解决问题。
【解决方案2】:

也许您遇到此问题是因为您以错误的方式设置刷新控件。不要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 中自定义默认行为并不是一个明智的决定。

【讨论】:

  • 对于 setBackgroundView 它以其他方式解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2013-12-11
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多