【问题标题】:UIRefreshControl with UICollectionView in iOS7iOS7 中带有 UICollectionView 的 UIRefreshControl
【发布时间】:2013-10-29 07:05:14
【问题描述】:

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

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 点,从而导致难看的滚动跳跃。

如果您将表与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];

但是UICollectionView没有这个属性,所以必须有办法手动处理。

【问题讨论】:

    标签: ios objective-c uitableview uicollectionview uirefreshcontrol


    【解决方案1】:
    - (void)viewDidLoad
    {
         [super viewDidLoad];
    
         self.refreshControl = [[UIRefreshControl alloc] init];
         [self.refreshControl addTarget:self action:@selector(scrollRefresh:) forControlEvents:UIControlEventValueChanged];
         [self.collection insertSubview:self.refreshControl atIndex:0];
         self.refreshControl.layer.zPosition = -1;
         self.collection.alwaysBounceVertical = YES;
     }
    
     - (void)scrollRefresh:(UIRefreshControl *)refreshControl
     {
         self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refresh now"];
         // ... update datasource
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Updated %@", [NSDate date]]];
            [self.refreshControl endRefreshing];
            [self.collection reloadData];
         }); 
    
     }
    

    【讨论】:

      【解决方案2】:

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

      这似乎正在发生,因为当您拉过滚动视图的边缘时,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 通过在您拉过刷新控件之前不会减慢跟踪速度来解决此问题。但是,我没有看到这种行为被暴露的方式。

      【讨论】:

      • 嗯,这肯定会让问题变得不那么明显。你能告诉我你是怎么得出这个计算的吗?它似乎不太准确。在我的手机上,当我乘以 1.7 而不是 3/2 时,我得到了最好的结果。但是 1.7 感觉比 3/2 还要随意。很高兴找出所需的确切调整...
      • 经过更多试验后,您的 3/2 似乎是对的。但是那个分数是从哪里来的???
      • 我通过查看手势识别器的翻译值与滚动经过 contentInset 点时滚动视图移动的差异之间的比率来凭经验确定它。它并不完美,我认为在 iOS 6 上它具有不同的价值。
      • Apple 说 UIRefreshControl 保证仅适用于 UITableViews 是否相关?
      • 非常适合我!但我注意到这个解决方案适用于 4" 显示器。对于 3,5" 有轻微的跳跃。修复了一些垃圾但有效的修复:translation.y -= diff * (screenHeight < 568. ? 3.5 : 3.0) / 2.0;
      猜你喜欢
      • 2013-06-20
      • 2013-11-01
      • 1970-01-01
      • 2023-03-25
      • 2019-11-04
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      相关资源
      最近更新 更多