【问题标题】:Detect direction of scrolling UICollectionView, load data from REST检测 UICollectionView 的滚动方向,从 REST 加载数据
【发布时间】:2013-04-19 19:34:31
【问题描述】:

假设标准配置(向上/向下),我想检测用户何时向上或向下滚动UIColletionView(这是UIScrollView 的子类并符合UIScrollViewDelegate)。我没有直接从代表那里看到任何信息来检测这一点,尽管我可能看多了一些东西。

如果我知道用户滚动的方向,那么我可以使用这些UICollectionViewDatasource 方法来确定我是否应该从 REST 服务器加载更多数据,或者清除我已经必须管理固定内存空间的信息。

// 如果向下滚动,则显示部分

- (UICollectionReusableView *)collectionView:(UICollectionView *)cv viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

// 如果向下滚动,则部分中的最后一个单元格消失

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

// 如果向上滚动,则显示部分中的最后一个单元格

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// 如果向上滚动,部分正在消失

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{

【问题讨论】:

标签: iphone ios ipad uicollectionview uiscrollviewdelegate


【解决方案1】:

您可以检查 UIScrollView(UICollectionView 继承自)的 panGestureRecognizer 属性并执行以下操作:

CGPoint scrollVelocity = [collectionView.panGestureRecognizer velocityInView:collectionView.superview];
if (scrollVelocity.y > 0.0f) {
    NSLog(@"going down");
} else if (scrollVelocity.y < 0.0f) {
    NSLog(@"going up");
}

Swift 3.1

let scrollVelocity = collectionView.panGestureRecognizer.velocityInView(collectionView.superview)
if (scrollVelocity.y > 0.0) {
    print("going down")
} else if (scrollVelocity.y < 0.0) {
    print("going up")
}

【讨论】:

  • 太棒了!正是我想要的。如果编辑不成功,只需意识到 velocityInView 返回一个 CGPoint 结构,而不是一个浮点数。如果您的集合视图是垂直 scoller,则需要检查 y 值。
  • @HCHogan 我试过了,但 scrollView 为空。你的观点应该是什么? UiCollectionView?
【解决方案2】:

你也可以使用这个:

CGPoint translation = [collectionView.panGestureRecognizer translationInView:collectionView.superview];
        if (translation.y > 0) {
            NSLog(@"DOWN");
        } else {

            NSLog(@"UP");
        }

更准确

【讨论】:

    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2016-02-29
    • 1970-01-01
    • 2018-09-01
    • 2011-11-01
    • 1970-01-01
    相关资源
    最近更新 更多