在 iOS7 中使用 UICollectionView 的 UIRefreshControl [英] UIRefreshControl with UICollectionView in iOS7

查看:17
本文介绍了在 iOS7 中使用 UICollectionView 的 UIRefreshControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application I use refresh control with 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 有一些令人讨厌的错误,当您下拉集合视图并且在刷新开始时不松开手指时,垂直 contentOffset 会向下移动 20-30 点,从而导致滚动跳转丑陋.

iOS7 has some nasty bug that when you pull collection view down and don't release your finger when refreshing begins, vertical contentOffset shifts for 20-30 points down which results in ugly scroll jump.

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

Tables have this problem too if you use them with refresh control outside of UITableViewController. But for them it could be easily solved by assigning your UIRefreshControl instance to UITableView's private property called _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];

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

But UICollectionView does not have such property so there must be some way to deal with it manually.

推荐答案

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

Having the same problem and found a workaround that seems to fix it.

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

This seems to be happening because the UIScrollView is slowing down the tracking of the pan gesture when you pull past the edge of the scrollview. However, UIScrollView is not accounting for changes to contentInset during tracking. UIRefreshControl changes contentInset when it activates, and this change is causing the jump.

在您的 UICollectionView 上覆盖 setContentInset 并考虑这种情况似乎有帮助:

Overriding setContentInset on your UICollectionView and accounting for this case seems to help:

- (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 刷新控件之前不会减慢跟踪速度来解决此问题.但是,我没有看到这种行为的暴露方式.

Interestingly, UITableView accounts for this by NOT slowing down tracking until you pull PAST the refresh control. However, I don't see a way that this behavior is exposed.

这篇关于在 iOS7 中使用 UICollectionView 的 UIRefreshControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆