检测滚动UICollectionView的方向,从REST加载数据

Vap*_*olf 4 iphone uiscrollviewdelegate ipad ios uicollectionview

假设标准配置(向上/向下),我想检测用户何时UIColletionView向上或向下滚动(这是子类UIScrollView和符合的类型UIScrollViewDelegate).我没有看到代表直接发现任何信息来检测这一点,尽管我可能已经在寻找一些东西了.

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

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

- (UICollectionReusableView *)collectionView:(UICollectionView *)cv viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
Run Code Online (Sandbox Code Playgroud)

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

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
Run Code Online (Sandbox Code Playgroud)

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Run Code Online (Sandbox Code Playgroud)

//如果向上滚动,部分就会消失

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
Run Code Online (Sandbox Code Playgroud)

Aou*_*yio 10

您可以检查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");
}
Run Code Online (Sandbox Code Playgroud)

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