在iOS7中使用UICollectionView的UIRefreshControl

Ily*_*hin 31 objective-c uitableview ios uicollectionview uirefreshcontrol

在我的应用程序中,我使用刷新控件和集合视图.

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

UIRefreshControl *refreshControl = [UIRefreshControl new];
[collectionView addSubview:refreshControl];
Run Code Online (Sandbox Code Playgroud)

iOS7有一些令人讨厌的错误,当你向下拉集合视图并且在刷新开始时不释放你的手指时,垂直contentOffset移动20-30点向下,这导致丑陋的滚动跳跃.

如果你在外面使用刷新控件,表也有这个问题UITableViewController.但是对于他们来说,可以通过将您的UIRefreshControl实例分配给UITableView名为的私有属性来轻松解决_refreshControl:

@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];
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*man 51

遇到同样的问题,发现似乎可以修复它的解决方法.

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

覆盖setContentInset你的UICollectionView并解释这个案例似乎有助于:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,UITableView通过不降低跟踪速度来解决这个问题,直到你将PAST刷新控制.但是,我没有看到这种行为暴露的方式.

  • 对我来说很完美!但我注意到这个解决方案适用于4"显示器.对于3,5",有一个轻微的跳跃.修复了一点点垃圾但工作修复:`translation.y - = diff*(screenHeight <568.?3.5:3.0)/ 2.0;` (4认同)