【发布时间】:2023-03-31 08:43:01
【问题描述】:
我正在尝试使用UIRefreshControl 在UITableView 中加载一些数据。我做到了,
- (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 行简单的代码即可设置UIRefreshControl。scrollViewDidScroll:方法中不需要任何代码。
标签: ios objective-c uitableview uirefreshcontrol