在UICollectionView框架外拖动单元格会更改其contentOffset

时间:2016-04-26 19:58:33

标签: ios objective-c uiscrollview drag-and-drop uicollectionview

当我在集合的框架之外拖动UICollectionViewCell时会发生这种错误:集合的contentOffset重置为0,我想要滚动到顶部,即使将单元格拖到集合下方也是如此。问题是必须手动将contentOffset放回到以前的位置并且在视觉上延迟。

我试图在拖动时锁定滚动,例如以下

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout willBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath {
    collectionView.scrollEnabled = NO;
}

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath {
    collectionView.scrollEnabled = YES;
}

它没有做任何事情,contentOffset仍在改变。还做了以下

- (CGPoint)collectionView:(UICollectionView *)collectionView targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset {

    if (proposedContentOffset.y > -10.0f) { // Minimum scroll from top is -10
        return CGPointMake(proposedContentOffset.x, -10.0f);
    }
    return proposedContentOffset;
}

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout willEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath {

    if (collectionView.contentOffset.y > -10.0f) { // Minimum scroll from top is -10
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x, -10.0f)];
    }
}

每当它改变时重置contentOffset并且这样可以正常工作,但是在拖动时更改了contentOffset,并且仅在用户释放单元格时才发生重置,因此会有延迟。我可以以某种方式在拖动时锁定contentOffset吗?

我认为值得一提的是我的视图结构目前正在

UIScrollView
    UICollectionView
    UICollectionView (the one that has drag-drop enabled)

父ScrollView统一了内部集合的两个卷轴,因此可能会出现问题。当集合被contentOffset滚动到顶部时,它会稍微侵入它上面的集合。

1 个答案:

答案 0 :(得分:0)

我意识到该项目使用LXReorderableCollectionViewFlowLayout框架来拖放UICollectionViews,所以我检查了源代码,发现处理拖出集合的方法是

- (void)handleScroll:(NSTimer *)timer

所以我在case LXScrollingDirectionUp添加了一点检查以获得最大边缘偏移量,我将其设置为该类的属性,如下所示

// distance calculated above: how much to scroll based on drag action
if (distance + contentOffset.y > _maxScrollingEdgeOffset.top) {
    distance = 0;
}

那就修好了!

相关问题