【发布时间】: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