简体   繁体   English

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

[英]Detect direction of scrolling UICollectionView, load data from REST

Assuming standard configuration (up/down), I'd like to detect when a user is scrolling their UIColletionView up or down (which is subclass of UIScrollView and conforms to UIScrollViewDelegate ). 假设标准配置(向上/向下),我想检测用户UIColletionView向上或向下滚动UIColletionView (这是UIScrollView子类并符合UIScrollViewDelegate )。 I don't see any information straight out of the delegate to detect this, although I may be over looking something. 我没有看到代表直接发现任何信息来检测这一点,尽管我可能已经在寻找一些东西了。

If I know which direction the user is scrolling, then I can use these UICollectionViewDatasource methods to determine if I should load more data from the REST server, or purge information that I already have to manage fixed memory space. 如果我知道用户正在滚动的方向,那么我可以使用这些UICollectionViewDatasource方法来确定是否应该从REST服务器加载更多数据,或者清除我已经拥有的管理固定内存空间的信息。

// If scrolling down, section is appearing //如果向下滚动,则显示部分

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

// If scrolling down, last cell in section is disappearing //如果向下滚动,则节中的最后一个单元格正在消失

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

// If scrolling up, last cell in section is appearing //如果向上滚动,则显示部分中的最后一个单元格

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

// If scrolling up, section is disappearing //如果向上滚动,部分就会消失

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

You can check UIScrollView's (which UICollectionView inherits from) panGestureRecognizer property and do something like this: 您可以检查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 : 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")
}

Also you can use this: 你也可以使用这个:

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

            NSLog(@"UP");
        }

More accurate 更准确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM