[英]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.