[英]swift: uicollectionview changes contentoffset of the cell during transition
[英]Dragging cell outside of UICollectionView frame changes its contentOffset
当我将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滚动到顶部时,它会稍微侵入其上方的集合。
我意识到该项目使用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;
}
这样就解决了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.