【问题标题】:Dragging cell outside of UICollectionView frame changes its contentOffset将单元格拖动到 UICollectionView 框架之外会更改其 contentOffset
【发布时间】:2023-03-07 15:15:01
【问题描述】:

当我将 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 滚动到顶部时,它会稍微侵入其上方的集合。

【问题讨论】:

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


    【解决方案1】:

    我意识到该项目使用 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;
    }
    

    这就解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多