如何使用刷新控件限制拉动距离?
How to limit the pulling distance with a refresh control?
我正在尝试使用 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];
}
}
使用此代码我可以将 table 视图下拉到屏幕底部。我的 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]
并将其作为 table 视图的背景视图。 Table 视图知道如何处理它。
self.myRefreshControl = [UIRefreshControl new];
[self.myRefreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.myTableView setBackgroundView:self.myRefreshControl];
这就是您通常设置下拉刷新控件的方式。但是如果这仍然不能满足你,你可以子类化 UIRefreshControl
这绝对不是那种情况下的最佳解决方案,或者你可以像 tnylee 建议的那样在 UIScrollView Delegate 方法中欺骗一些东西。但是在 scrollViewDidScroll
中自定义默认行为并不是一个明智的决定。
我正在尝试使用 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];
}
}
使用此代码我可以将 table 视图下拉到屏幕底部。我的 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]
并将其作为 table 视图的背景视图。 Table 视图知道如何处理它。
self.myRefreshControl = [UIRefreshControl new];
[self.myRefreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.myTableView setBackgroundView:self.myRefreshControl];
这就是您通常设置下拉刷新控件的方式。但是如果这仍然不能满足你,你可以子类化 UIRefreshControl
这绝对不是那种情况下的最佳解决方案,或者你可以像 tnylee 建议的那样在 UIScrollView Delegate 方法中欺骗一些东西。但是在 scrollViewDidScroll
中自定义默认行为并不是一个明智的决定。