ios - 在 iOS7 中使用 UICollectionView 的 UIRefreshControl

标签 ios objective-c uitableview uicollectionview uirefreshcontrol

在我的应用程序中,我使用带有 Collection View 的刷新控件。

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds];
collectionView.alwaysBounceVertical = YES;
...
[self.view addSubview:collectionView];

UIRefreshControl *refreshControl = [UIRefreshControl new];
[collectionView addSubview:refreshControl];

iOS7 有一些讨厌的错误,当您下拉 Collection View 并且在刷新开始时不松开手指时,垂直 contentOffset 会向下移动 20-30 点,从而导致难看的滚动跳转。

如果您在 UITableViewController 之外使用带有刷新控制的表,它们也会有这个问题。但是对于他们来说,可以通过将您的 UIRefreshControl 实例分配给 UITableView 的名为 _refreshControl 的私有(private)属性来轻松解决:

@interface UITableView ()
- (void)_setRefreshControl:(UIRefreshControl *)refreshControl;
@end

...

UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:tableView];

UIRefreshControl *refreshControl = [UIRefreshControl new];
[tableView addSubview:refreshControl];
[tableView _setRefreshControl:refreshControl];

但是 UICollectionView 没有这样的属性,所以必须有一些方法来手动处理它。

最佳答案

遇到同样的问题并找到了似乎可以解决的解决方法。

这似乎是因为当您拉过 ScrollView 的边缘时 UIScrollView 正在减慢对平移手势的跟踪。但是,UIScrollView 不考虑跟踪期间对 contentInset 的更改。 UIRefreshControl 在激活时更改 contentInset,此更改导致跳转。

覆盖 UICollectionView 上的 setContentInset 并解决这种情况似乎有帮助:

- (void)setContentInset:(UIEdgeInsets)contentInset {
  if (self.tracking) {
    CGFloat diff = contentInset.top - self.contentInset.top;
    CGPoint translation = [self.panGestureRecognizer translationInView:self];
    translation.y -= diff * 3.0 / 2.0;
    [self.panGestureRecognizer setTranslation:translation inView:self];
  }
  [super setContentInset:contentInset];
}

有趣的是,UITableView 通过在您拉 PAST 刷新控件之前不减慢跟踪来解决这个问题。但是,我看不到这种行为的暴露方式。

关于ios - 在 iOS7 中使用 UICollectionView 的 UIRefreshControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19483511/

相关文章:

ios - Storyboard中多个 View Controller 使用的公共(public) View

objective-c - 从 uisplitview 更改主视图和详细 View

objective-c - 如何在 native ios 代号一中包含资源?

ios - 使表格 View 内的 UITextField 可见滚动

swift - 在检查一个 swift 2 之前无法取消检查所有单元格

ios - 为什么我的自定义 UITableViewCell 没有显示?

ios - 有没有一种方法可以使用 SDWebImage 将 1000 张图像预加载到缓存中,而无需在屏幕上实际显示它们?

iphone - NSURL 连接和 json

objective-c - NSCell 重绘问题

ios - Xamarin Forms - 如何使用标题制作 iOS ListView native 分组样式?