将单元格拖到 UICollectionView 框架之外会更改其内容偏移量



当我将UICollectionViewCell拖到集合框架之外时,会发生此错误:集合的内容偏移量重置为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;
}

它什么也没做,内容偏移量仍然在变化。还做了以下工作

- (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)];
    }
}
在内容偏移量更改时重置

它,这工作正常,但是在拖动时会更改内容偏移量,并且仅在用户释放单元格时重置,因此存在延迟。我可以在拖动时以某种方式锁定内容偏移量吗?

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

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

父 ScrollView 统一了内部集合的两个滚动,因此这可能是一个问题。当集合按 contentOffset 滚动到顶部时,它会稍微侵入其上方的集合。

我意识到该项目正在使用 LXReorderableCollectionViewFlowLayout 框架在 UICollectionView 上进行拖放,所以我检查了该源代码,发现处理拖出集合

的方法
- (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;
}

这就解决了!

最新更新