[英]Detect direction of scrolling UICollectionView, load data from REST
假设标准配置(向上/向下),我想检测用户UIColletionView
向上或向下滚动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{
您可以检查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")
}
你也可以使用这个:
CGPoint translation = [collectionView.panGestureRecognizer translationInView:collectionView.superview];
if (translation.y > 0) {
NSLog(@"DOWN");
} else {
NSLog(@"UP");
}
更准确
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.